- Home /
Pause menu Script not working
Hey guys i was writing a script to pause the game and open the pause menu but my script doesn't seem to work properly, i added a debug to print the timescale and it prints 0 but doesn't pause the game. I haven't been able to figure out why. can someone please help me?
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public GameObject PauseUI;
public static bool Paused = false;
void Resume()
{
PauseUI.SetActive(false);
Time.timeScale = 1f;
Paused = false;
}
void Pause()
{
PauseUI.SetActive(true);
Time.timeScale = 0f;
Paused = true;
Debug.Log(Time.timeScale);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Paused == false)
{
Pause();
}
if (Paused == true)
{
Resume();
}
}
}
public void BackToMenu()
{
SceneManager.LoadScene("GameMenu");
}
}
Answer by yummy81 · Sep 07, 2018 at 10:58 AM
The problem with your code lies here, in the Update method:
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Paused == false)
{
Pause();
}
if (Paused == true)
{
Resume();
}
}
}
So, when you press the Escape button, your code checks whether the Paused boolean is false. If it is, then it invokes the Pause method. Inside Pause method, the Paused boolean is assigned true and timeScale is assigned 0f. Subsequently, your code exits the Pause method and comes back to the Update method where - and this is where the problem lies - it checks once again if the Paused boolean is true. Because it is true, then the Resume method is invoked, where the Paused boolean is assigned false, and timeScale is assigned 1f. And that's why it do not work. The reason why you see only zero in the console is that, you added Debug.Log only in the Pause method. Try to add it in the Resume method, and you will see that those two methods are invoked simultaneously. It's very easy to make it work. Simply add "else" before "if (Paused == true)":
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (Paused == false)
{
Pause();
} else if (Paused == true)
{
Resume();
}
}
}
Answer by KittenSnipes · Sep 07, 2018 at 02:30 AM
@Mindrobot It is because you never actually use the Boolean to pause or resume
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public GameObject PauseUI;
public static bool Paused = false;
void Resume()
{
PauseUI.SetActive(false);
Time.timeScale = 1f;
}
void Pause()
{
PauseUI.SetActive(true);
Time.timeScale = 0f;
Debug.Log(Time.timeScale);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Paused = !Paused;
}
if (Paused == false)
{
Pause();
}
if (Paused == true)
{
Resume();
}
}
public void BackToMenu()
{
SceneManager.LoadScene("GameMenu");
}
}
Answer by Sonicbulletwolf · Sep 07, 2018 at 02:36 AM
Hope this helps!
[SerializeField] // See in Inspector
private GameObject PauseUI; // Game Object
[SerializeField]
private bool gamePaused; // Enable / Disable
void EnablePauseMenu()
{
Time.timeScale = 0f;
PauseUI.SetActive(true);
}
// We don't need to set the variable up
// Since we already switch it every
// Time we Hit 'Esc' it switches
// for us!
void DisablePauseMenu()
{
Time.timeScale = 1f;
PauseUI.SetActive(false);
Debug.Log(Time.timeScale);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
/// This snipit of codes changes the variable
/// to be the opposite of what is was
/// such as if I hit 'Esc' if its false
/// it turns to true
gamePaused = !gamePaused;
}
// Same as gamePause == true
// (!gamePaused) is same as gamePause == false
// Checking if its true and paused already switch
// it to false, else if its not true then enable
// the UI.
if (gamePaused)
{
// Checking
DisablePauseMenu();
}
else
{
EnablePauseMenu();
}
}
public void BackToMenu()
{
SceneManager.LoadScene("GameMenu");
}
Answer by Mindrobot · Sep 07, 2018 at 12:04 PM
Thank you @yummy81 , @Sonicbulletwolf and @KittenSnipes a lot for helping me understanding the many problems in my code. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612174201im_/https://answers.unity.com/themes/thub/images/avi.jpg)