Close my GUI button by repressing the same Hot-key.

I have a couple of buttons that pop up when you press a specific hot key and then when you press the GUI button, it does a specific action. For example, when I press escape, it brings up a "Quit" button which brings you back to the main screen when you click it. The problem I'm having is when I press Esc again, i'd like the GUI button to disappear, you know, toggle it on and off. Here's the JS code i have for it now, I don't know what to change to actually get it to work, I've tried putting things like quitMenu = !(quitMenu); and other variations of the code, but nothings working. It either disables the button entirely, or just ignores the code.

private var  quitMenu;

function OnGUI () {
if (Input.GetButtonDown ("Esc")) { quitMenu=true;}
if (quitMenu == true)
{
if (GUI.Button (Rect(10,10,50,50),"Quit"))
{ Application.LoadLevel("Opening Screen");

}

}

}

If anyone can help, I'd be greatly appreciative.

Here is a more complete example written in `JavaScript`. Just copy/paste it as it is and assign it to an `Empty Object` to see this example in action.

var width: int = 256;
var height: int = 256;
var skin: GUISkin;

private var rect: Rect;
private var show: boolean = false;

function Awake() {
    var x = (Screen.width * 0.5) - (width * 0.5);
    var x = (Screen.height * 0.5) - (height * 0.5);
    rect = Rect(x, y, width, height);    
}

function Update() {
    if (Input.GetKeyDown(KeyCode.Escape)) {
        show = !show;
    }    
}

function OnGUI() {
    GUI.skin = skin;    
    if (show) {
        GUILayout.BeginArea(rect);
        GUILayout.Box("Pause Menu");
        if (GUILayout.Button("Continue")) {
            show = false;
        }
        if (GUILayout.Button("Restart")) {
            Application.LoadLevel("Current Level");
        }
        if (GUILayout.Button("Quit")) {
            Application.LoadLevel("Opening Screen");
        }
        GUILayout.EndArea();
    }
}

Now in more details. From the inspector you are able to change the `width` and `height` of your menu that will be displayed in the center of the screen (the code for that is in the `Awake()` function). In order to draw the `GUI` I'm using the `Button` and `Box` functions from `GUILayout`. For a better user experience you can also create a `GUISkin` and assign it to the `skin` variable in the `Inspector`.

Good luck and have fun!

This is off the top of my head, not tested...


private var quitMenu;

function OnGUI () {

if (Input.GetButtonDown ("Esc")) 
{ 
    if (quitMenu == true) { quitMenu = false; }
    else { quitMenu=true; }

}

if (quitMenu == true)
{
    if (GUI.Button (Rect(10,10,50,50),"Quit"))
    { 
        Application.LoadLevel("Opening Screen");

    }

}

}

Well, good news, this solution is tested :) I was curious why it didn't work, the logic seemed right. Turns out, for whatever reason, having an Input() in the OnGUI() call is a bad idea. I was getting double-clicks of my escape key. By moving Input() down to Update(), it worked fine. Also note I used GetKeyUp(KeyCode.Escape)

The actual complete (C#) code. Note that this should be in a file called *s_GUI.cs*, to match the Class name:

using UnityEngine;
using System.Collections;

public class s_GUI : MonoBehaviour {
bool quitMenu = false;

    void OnGUI() {
        if (quitMenu == true) {
            if (GUI.Button (new Rect(20, 20, 20, 20), "Quit")) {
                quitMenu = false;
                // application stuff.
            }
        }
    }

    void Update () {
        if (Input.GetKeyUp(KeyCode.Escape)) {
            quitMenu = !quitMenu;
        }
    }
} // end class s_GUI