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?