Pause Menu

Hello fellow Programmers,

I started coding with Unity about 2 Weeks ago and I am currently working on making one of the tutorial projects (Roll a Ball) my own and creating it by myself. I have encountered whilst working on the pause menu something I don’t really understand so I have to ask here:
I tried this first, but it didn’t work (No compiler errors!) so I tried it with a button which then worked (I want it to be accessed through the key “Escape” as well)

public GameObject PauseMenu;

void Start () {
        PauseMenu.SetActive (false);
    }

    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Escape)) {
            if(Time.timeScale == 1) {
                Time.timeScale = 0;
                PauseMenu.SetActive (true);
                Cursor.visible = true;      
            } else {
                Time.timeScale = 1;
                PauseMenu.SetActive (false);
                Cursor.visible = false;      
            }
        }
    }

here the working code, binded to a button:

public GameObject PauseMenu;


void Start () {
    PauseMenu.SetActive (false);
}

public void PauseButton()
    {
        if (Time.timeScale == 1) {
            PauseMenu.SetActive (true);
            Time.timeScale = 0;
            Cursor.visible = true;
        }
        else
        {
            PauseMenu.SetActive (false);
            Time.timeScale = 1;
            Cursor.visible = false;
        }
    }

I thank anybody for their help!

Happy coding

The Problem here is that u can’t press the Esc Button short enough. Pls type “Debug.Log(“Esc Click”);” into your first if. The you can see the Problem.

Try GetKeyUp or GetButtonDown to fix the Problem.

//Edit:
If this is not working, try “PauseMenu.SetActive(false);” to replace with “PauseMenu.enabled=false;”.
An other thing is the “public GameObject PauseMenu;”.
Any GUI is working with a Canvas like in your hierarchy. So replace GameObject with Canvas.

Thank you so much!!

First I tried it with GetKeyUp and GetButtonDown, but it still did not work … but when I tried the Tipps you put in the edit it worked!

So I am not sure what caused the problem, but I chill check now :slight_smile: