- Home /
How do I make a GUI menu that pops up?
How do I make a GUI menu that appears when I press a certain button, and disappears when I press another? It's a simple question. Thanks in advance!
Comment
Answer by ExpiredIndexCard · Mar 25, 2013 at 03:11 AM
Example:
bool isOpen = false;
if(Input.GetKeyDown("escape")){
//Show the screen
isOpen = true;
}
if(Input.GetKeyDown("escape")){
if(isOpen)
//Don't show the screen
}
Answer by Chronos-L · Mar 25, 2013 at 09:09 AM
public class ToggleGUI : MonoBehaviour {
private bool on = false;
void Update() {
//Use one of these 3 ways to toggle your menu!
//Type 1: Use the same key to on/off
if( Input.GetKeyDown( KeyCode.Q ) ) {
on = !on;
}
//Type 2: Use one key to turn on
// another key to off
if( Input.GetKeyDown( KeyCode.Q ) ) {
on = true;
}
else if( Input.GetKeyDown( KeyCode.E ) ) {
on = false;
}
//Type 3: Hold a key to keep the menu open
if( Input.GetKeyDown( KeyCode.Q ) ) {
on = true;
}
else if( Input.GetKeyUp( KeyCode.Q ) ) {
on = false;
}
}
void OnGUI() {
if( on ) {
...
}
}
}
It is a bit of a overkill, but I wrote in 3 difference menu-toggle-by-key behaviours. Use whichever one you prefer.
Sir, you just made my day...
I've been looking at what I was doing wrong but looking back at it.. It was such a silly mistake of $$anonymous$$e.. really thank you!
Your answer
![](https://koobas.hobune.stream/wayback/20220613095028im_/https://answers.unity.com/themes/thub/images/avi.jpg)