P
P
piffo2020-05-16 15:10:34
C++ / C#
piffo, 2020-05-16 15:10:34

Endless repetition?

private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (bol == false)
            {
                Pause();
                return;
            }
            if (bol == true)
            {
                Resume();
            }
        }
    }
 
    public void Resume()
    {
        for (int i = 0; i < 5; i++)
        {
            but[i].SetActive(false);
        }
        Time.timeScale = 1f;
        bol = false;
    }
 
    void Pause()
    {
        for (int i = 0; i < 5; i++)
        {
            but[i].SetActive(true);
        }
        Time.timeScale = 0f;
        bol = true;
    }

When Pause bol becomes true, if bol becomes true then Resume is started, when Resume bol becomes false, if bol becomes false then Pause is started. And so endlessly. How can I make it so that when you press Esc, bol becomes true, the next time you press bol becomes false, then true again, and so on. But that's not all.)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2020-05-16
@piffo

.. I read the comments .. well, now I definitely won’t pass by ..

Endless repetition?
there is a fundamental principle for organizing loops, any - provide an exit condition
is necessary and enough one exit condition , but it must be reliable
.. everything else is vanity from scratch
.. although the real logic can be more complicated .. but only when you have mastered principle , and fulfilled it ..
by code
замените
if (bol == false) {}
if (bol == true) {}
на
if (bol) {} 
else {}

тогда легче заметить, bol у вас тут не определено
чисто интуитивно (если бы не коварный bol) напрашивалось бы примерно так:
if (Input.GetKeyDown(KeyCode.Escape))
      Pause();
else
      Resume();

или наоборот, но это лишь один символ
if (!Input.GetKeyDown(KeyCode.Escape))
      Pause();
else
      Resume();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question