GUI Problems with buttons

hey, i try to make a little menu that pops up when i press the Escape key. I looked it up in other questions, but some how this is not working!

        if(Input.GetKeyDown(KeyCode.Escape)) {
                escape = true;
        }
        else{
                escape = false;
        }

}
function OnGUI () {
    if(escape == true) {
        GUI.BeginGroup(Rect(Screen.width / 2 - 550, Screen.height / 2 - 100, 200, 100));
        GUI.Box(Rect(0,0,200,100),"PAUSE");
        GUI.EndGroup ();
}

}

i know that the var escape is not necassary, but i always think that booleans are a nice way to see if the code is working(u can see if the get changed in the inspector). can u guys help me out?

2 Answers

2

The problem is, you hide the menu every frame the escape wasn't pressed - basically your menu shows for one frame

Try this instead:

    if(Input.GetKeyDown(KeyCode.Escape)) {
            escape = !escape;
    }

Which will toggle each time you hit escape

also another note before "Rect" put "new" I don't know why but in C# unity likes "new" before "Rect" when dealing with GUI.