- Home /
GUI buttons with alternate activation condition
I have some gui buttons for a menu but id like them to be able to be hot keyed essentially but for the gamepad.
Ive tried successfully:
if (GUI.Button(new Rect(10, 70, 50, 30), "Click") || Input.GetKeyDown(KeyCode.Space))
//Some Code
But this does not work:
if (GUI.Button(new Rect(10, 70, 50, 30), "Click") || Input.GetKeyDown(KeyCode.JoystickButton0))
//Some Code
Clicking the button is working normally but the right side isn't evaluating to True. Is there some reason behind the scenes to cause this? And yep the gamepad does work, have lots of other code using KeyCode.JoystickButtonX
Edit: Found Issue: Joystick input is not recognised inside OnGUI function.
$$anonymous$$aybe you have some kind of conflicts with your input. If any of your other scripts are also detecting JoyStick button commands this could interfere with your OnGUI buttons()
You should try to have all your user input Button stuff in one update function I think.
Answer by Tomer-Barkan · Nov 04, 2013 at 06:48 AM
No reason whatsoever. This is just a simple OR statement, there's no reason it wouldn't work, regardless of what the right-part of the if statement includes.
Just for the sake of debugging, try the following code instead:
bool clicked = GUI.Button(new Rect(10, 70, 50, 30), "Click"));
bool joystick = Input.GetKeyDown(KeyCode.JoystickButton0);
Debug.Log("clicked: " + clicked + ", joystick: " + joystick);
if (clicked || joystick) {
Debug.Log("Inside if statement");
// rest of code
}
And see what the log prints.
Thanks Ill try this out when I get home later, maybe expand a few more scenarios too.
Also btw the button click still works, just the right side never seemed to evaluate to true. $$anonymous$$y question may have implied that the regular usage of clicking the button stopped working too.
I think that code will execute regardless of what GUI button is pressed, if you press the joystick button it will run.
I would try using it in this format:
if(GUI.Button(...)){
if(Input.Get$$anonymous$$eyDown(...)){
//code
}
}
Although this might execute regardless as well...
@Josh707, this changes what he's trying to do, as it requires both the button and the joystick to be pressed, and he wants either.
@xt.xylophone, yes it was clear from your question that the joystick worked well in other sections. I can't figure whats wrong with your code that's why I suggested doing some debugging. Probably something small and silly that we're missing.
Good find.
Just have two if statements, one in the gui to check if the button Is pressed and an if statement in the update to check joystick input. They can both call the same class method so you dont duplicate Code.