- Home /
This post has been wikified, any user with enough reputation can edit it.
How do I open a GUI on key press?
When I press a key (in this case, 'q') I want a GUI to open up, like a shop. So far it logs to the console correctly, but no GUI is showing. Here's my code:
// JavaScript
function OnGUI () {
GUI.color = Color.green;
GUI.Box (Rect (10,10,100,90), "CONTROLS");
GUI.Box (Rect (15,30,90,50), "Q: OPENS \n SHOP");
if(Event.current.Equals(Event.KeyboardEvent("Q"))) {
Debug.Log("Shop opened!");
GUI.Box (Rect (50,25,300,300), "SHOP");
}
}
Comment
Best Answer
Answer by konashadow · May 29, 2014 at 02:40 AM
Instead of using the event, I just created a boolean that flips if "q" is pressed. Here's what it looks like.
#pragma strict
var shop : boolean;
function Start () {
shop = false;
}
function Update () {
if(Input.GetKeyDown(KeyCode.Q)) {
shop = !shop;
}
}
function OnGUI () {
GUI.color = Color.green;
GUI.Box (Rect (10,10,100,90), "CONTROLS");
GUI.Box (Rect (15,30,90,50), "Q: OPENS \n SHOP");
if(shop) {
//Debug.Log("Shop opened!");
GUI.Box (Rect (50,25,300,300), "SHOP");
}
}
Hope this helps!