Hello!I’ve been trying to figure out how can i assign a hotkey to my GUI buttons. I’ve managed to make the button create a Box when pressed by the mouse and then close it and also managed somehow to make it open with the I key but i cannot close it with it. Also i cannot open with mouse and then close with I. Here is the script i use:
var charPressed = false;
var pushkey = false;
function OnGUI() {
if( GUI.Button (new Rect (Screen.width - 118,Screen.height - 120,110,30 ), "Inventory" ) ) {
charPressed = !charPressed;
}
if( charPressed ) {
GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
}
if(Input.GetKeyUp(KeyCode.I)) {
pushkey = true;
}
if(pushkey == true) {
GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
}
}
just change:
if(Input.GetKeyUp(KeyCode.I)) {
pushkey = true;
}
TO:
if(Input.GetKeyUp(KeyCode.I)) {
charPressed = !charPressed;
}
Also, you may need to move your Input code to Update()
If both GUI.Button and Input.GetKey() is affecting the same GUI.Box, then they should affect the same boolean value.
var showBox = false;
function Update() {
if(Input.GetKeyUp(KeyCode.I)) {
showBox = !showBox;
}
}
function OnGUI() {
if( GUI.Button (new Rect (Screen.width - 118,Screen.height - 120,110,30 ), "Inventory" ) ) {
showBox = !showBox;
}
if( showBox ) {
GUI.Box( Rect( 350, 120, 300, 110 ), "INVENTORY" );
}
}
Your question title is misleading, it makes me think about dynamic key binding to gui button during run time, which is a much more complicated subject.
i made a mod awhile back still have the mod and im trying to make my gui.buttons to work off keybinds but its being a pain my mod uses visual studio please help,how do you make the gui.button close and open with a keybind not the box