- Home /
Pause Menu problem
Hey everyone I have a simple pause menu for my game which comes up when I press escape key. It has two buttons - to resume the game and to quit and return to main menu. Both buttons work fine but when I return to the main menu using the quit button and go back to play the race level again, the scene is already paused. The only way to go forward is by pressing the escape key again, which brings up the pause menu and then by pressing resume the game works as normal. Attached below is the code for the menu -
public class PauseMenu : MonoBehaviour { public bool gamePaused = false; public GameObject pauseMenu;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Cancel")){
if (gamePaused == false) {
Time.timeScale = 0;
gamePaused = true;
pauseMenu.SetActive(true);
}
else {
pauseMenu.SetActive(false);
gamePaused = false;
Time.timeScale = 1;
}
}
}
public void UnpauseGame (){
pauseMenu.SetActive(false);
gamePaused = false;
Time.timeScale = 1;
}
public void ToManinMenu(){
gamePaused = false;
SceneManager.LoadScene(2);
}
}
Can someone point out if I am missing something or if there is something wrong with my script? Any help would be greatly appreciated.
Answer by Okido · Jul 12, 2020 at 04:03 PM
Time.timeScale is universal, so it won't be reset to 1 when you load a new scene. At some point between pressing the Main Menu button and loading into the next game, you need to set the timescale to 1 (I would personally do it when loading the main menu for this)