1
\$\begingroup\$

So I think I have a bug with my pause menu. For the sake of simplicity I've boiled down what I'm trying to acheive: When the player first starts up the game there's a UI menu with one button: 'Start Game' that they have to press to start the game. They can then pause and bring up this menu again at any time by pressing space. The player can resume the game either by pressing 'Start Game' or by pressing space again.

The problem right now is that when the player presses space again to unpause, nothing happens - unless they click anywhere on the screen. It's sort of complicated to explain but I'll try my best.

Say the player previously started or unpaused the game by clicking the 'Start Game' button. If they pause the game again, they can only unpause the game by pressing space if they click somewhere on the screen. This works if the player clicks somewhere on the screen before or after they press pause.

However, If the player previously unpaused the game by pressing space, they can pause and unpause by pressing space indefinitely (until they unpause by pressing 'Start Game'). Is this a bug with the Unity UI system (I'm using Unity 5.0)? Or maybe with my code?

Here's my code:

public class GameController : MonoBehaviour 
    {bool paused;
    public GameObject canvas; //in the editor I set this to reference the canvas that
                              //the 'Start Game' button is on

    void Awake()
        {PauseGame ();}

    void Update()
        {if (Input.GetKeyDown (KeyCode.Space)) 
            {PauseGame ();
            Debug.Log ("Pressed 'Space'");}
        }

    public void PauseGame()
        {paused = !paused;
        if (paused == true)
            {Time.timeScale = 0;
            canvas.SetActive (true);
            Debug.Log ("'Pause' Called");}

        else
            {Time.timeScale = 1;
            canvas.SetActive (false);
            Debug.Log ("'Unpause' Called");}
        }
    }

*The 'Start Game' button calls the PauseGame() method from the GameController script

Update

I wrote some debugging lines to figure out what was happening. Apparently, if the player previously unpauses by pressing 'Start Game' then pauses again, when they try to unpause by pressing space the game does indeed unpause but then immediately pauses again. However it only does this if the player previously unpaused by pressing 'Start Game' (and can be remedied by clicking somewhere on the screen before or after pausing). This doesn't happen if the player previously unpaused by pressing space.

Update

Apparently the 'Unpause' is being called by the event system, and the 'Pause' is being called by pressing space. I guess Unity has it built in that if a player previously clicked a UI button, pressing space will click that button again when the option is available. Does anyone know how to alter this?

\$\endgroup\$
  • 1
    \$\begingroup\$ What you're asking here isn't entirely clear. For one, the title "weird bug" doesn't tell someone browsing the issue list anything about your topic. Then you say you can't unpause by pressing space "unless I had previously unpaused by pressing space" - so, unpausing by pressing space sometimes works without clicking and sometimes doesn't? Maybe try putting together a simple minimal test case that exhibits the issue, with as few dependencies on your other game systems like muting as possible. \$\endgroup\$ – DMGregory Apr 14 '15 at 18:41
  • \$\begingroup\$ Thank you for your feedback! I'll do some editing to try and make my question more clear \$\endgroup\$ – Josie Thompson Apr 14 '15 at 20:19
4
\$\begingroup\$

I solved it myself! Sorry that I had to take up space in the GameDev stack exchange site but I guess if anyone else has this problem they'll know how to solve it. The problem was with the event system. If the 'Send Navigation Events' bool is true under the 'Event System' script component of the 'Event System' game object then the game object of the button last clicked will be considered 'selected' and pressing space will click that button again.

Basically what was happening was that pressing space was both pausing and unpausing the game because the 'Start Game' button was "selected." Pressing space called the function on that button as well as the the function in the Update() message in the GameController script. This can be solved by unchecking the "Send Navigation Events" box on the Event System.

\$\endgroup\$
  • 2
    \$\begingroup\$ Thank you for posting the answer now that you've solved it! This is exactly how the system is supposed to work, you didn't take up any space at all. Don't forget to mark this as accepted when you can. \$\endgroup\$ – Jesse Dorsey Apr 16 '15 at 1:50
0
\$\begingroup\$

Try this: start your game in the editor, click on the play window, then (without clicking off the window) use space to pause the game. Then immediately hit space again to unpause.

If this works, then you're fine. What you're seeing is the fact that Unity play windows don't get keystrokes (or mouseclicks, for that matter) unless they have focus.

\$\endgroup\$
  • \$\begingroup\$ What do you mean by the play window? Do you mean the game window? I think I understand what you mean by they don't get keystrokes unless they have focus. This is why I thought it might be a unity bug. Because I never click out of the game window. If the problem is caused by this then the game is unfocusing itself whenever it gets paused \$\endgroup\$ – Josie Thompson Apr 14 '15 at 20:42
  • \$\begingroup\$ Yes, the game window is what I meant. Glad to see you figured it out! \$\endgroup\$ – Tim Keating Apr 16 '15 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.