- Home /
Pausing game by monitoring player script for start button being pressed
Hi, I've built a platform game that works exactly how I want, but I'm trying to tidy up the code a bit. I've run into a problem that I can't understand.
I have a script that, amongst other things, listens for the player pressing the start button. When this happens the script pauses the game. When the player pressed start again, the game unpauses. The scipt is something like this:
void Update ()
{
if (Input.GetButtonDown ("Start"))
PauseGame ();
}
void PauseGame () // Pauses/unpauses the game.
{
if (Time.timeScale > 0) // If the game isn't paused,
{
Time.timeScale = 0; // stop time,
gamePaused = true; // signal that the game is paused,
cam.pauseScreen.SetActive(true); // show the pause overlay,
AudioListener.pause = true; // pause any audio.
}
else if (Time.timeScale == 0) // If the game is paused,
{
Time.timeScale = 1; // restart time,
gamePaused = false; // signal that the game isn't paused,
cam.pauseScreen.SetActive(false); // hide the pause overlay,
AudioListener.pause = false; // unpause the audio.
}
}
However I'm trying to neaten things up by having my player controller script handle all player commands. So I've created a public variable in the player controller called startPressed:
startPressed = Input.GetButtonDown ("Start");
and I want to use this variable in the pausing script. Something like this:
void Update ()
{
if (player.startPressed)
PauseGame ();
}
But this doesn't work. The pause script executes, but the game immediately unpauses again. Anyone got any idea why this is happening?
Thanks a lot :)