Home > Back-end >  Weird behaviour when I try to Pause/Unpause Game with same button in Unity
Weird behaviour when I try to Pause/Unpause Game with same button in Unity

Time:06-22

I am trying to Pause and Unpause the game using the ESCAPE button, but I encounter a strange bug.

Update.png Pause-UnpauseGame.png

When I have the code like this, it behaves how it's supposed to - It pauses the game when I press the pauseKey(ESC).

If I uncomment the commented line in the Unpause method, the one that should hide the UI and set Time.timescale back to 1, (almost) everything breaks. The functionality from the PauseGame method that previously worked doesn't work anymore. When I uncomment the Debug.Log calls from the PauseGame method, the correct values are displayed, but nothing happens. The game isn't frozen although it says that Time.timeScale is 0.

DebugLog.png

These are the rest of the methods that are referenced in the screenshots posted above:

OnResumePressed.png CloseAllMenus.png

Any idea, piece of guidance, or reliable source that you think might help is very welcomed. Thank you for your time and attention!

CodePudding user response:

Your code is pausing the game then the next line unpauses (the now paused) game. Make it an if () {} else {} and it should work ok.

  • Related