- Home /
Make GUI appear on key down
How do I make a GUI appear when a button is pressed? I want to present the player with a list of options they can choose from at any given time.
void OnGUI(){
if(Input.GetKeyDown(KeyCode.LeftShift)){
GUI.Button(new Rect(10,200,1200,100), "Forage");
GUI.Button(new Rect(10,400,1200,100), "Rest");
GUI.Button(new Rect(10,600,1200,100), "Wait");
}
Answer by fafase · Feb 26, 2014 at 05:01 AM
Input should not be used in OnGUI, the alternative is to use Event or Input in Update with a boolean:
bool guiOn = false;
void Update(){
if(Input.GetKeyDown(KeyCode.LeftShift))guiOn = !guiOn;
}
void OnGUI(){
if(guiOn){
// Your code
}
}
using event:
bool guiOn = false;
void OnGUI(){
Event e = Event.current;
if (e.keyCode == KeyCode.LeftShift)guiOn = !guiOn;
if(guiOn){// Your code}
}
The reason comes from how both are handled, Input is based on the operating system while the OnGUI can be run once or more per frame so it is not related to the frame rate. All in all, Input in Update, event in OnGUI (event is not available outside OnGUI).
Your answer
Follow this Question
Related Questions
GUI buttons with alternate activation condition 1 Answer
How do I get a GUI Texture Button to act as a Input Key such as a T button on a key bored or so? 1 Answer
Editor Scripting Input Problem OnGUI() 1 Answer
How to get mouse position with ray from camera??? 1 Answer
Backspace in limited length textfield causes ArgumentOutOfRangeException 1 Answer