- Home /
Question by
saurabhn04 · Mar 21, 2014 at 03:51 PM ·
inventory
I am trying to make a GUI visisble after pressing a key
I am trying to make an inventory(GUI) to appear if I press the "I" key. But when I do press "I" the inventory only appears as long as keep "I" pressed. But I want to make the GUI visible if I press the "I" key once and when I press it again it closes. Is it possible.
The script is:
function OnGUI(){
//Health bar
GUI.Box(Rect(10,10,500,30),"100/100");
//inventory is displayed when key "I" is pressed
if(Input.GetKey(KeyCode.I)){
GUI.Box(Rect(10,50,300,450),"Inventory");
GUI.BeginGroup(Rect(Screen.width/25-50, Screen.height/9-50,300,450));
GUI.Button(Rect(13,53,70,70),"Item");
GUI.EndGroup();
}
}
Thanks
Comment
use a bool that if I is pressed turn it on, and if the bool var is on show your inventory, question already asked too many times
Answer by Professor Snake · Mar 21, 2014 at 03:54 PM
Input.GetKey(KeyCode.I) returns true every frame the I key is pressed. You're instead going to have to use a boolean that gets toggled every time you press I. GetKeyDown is better suited to this purpose since it only returns true on the frame it was pressed.
function Update(){
if(Input.GetKeyDown(KeyCode.I))
showGUI=!showGUI;
}
function OnGUI(){
if(showGUI){
//GUI code here
}
}