Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by xt-xylophone · Nov 04, 2013 at 02:59 AM · guiinputcontrollerkeycode

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RyanZimmerman87 · Nov 04, 2013 at 09:09 PM 0
Share

$$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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xt-xylophone · Nov 04, 2013 at 07:53 PM 0
Share

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.

avatar image Josh707 · Nov 04, 2013 at 08:07 PM 0
Share

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...

avatar image Tomer-Barkan · Nov 04, 2013 at 08:50 PM 0
Share

@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.

avatar image Tomer-Barkan · Nov 04, 2013 at 08:51 PM 0
Share

@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.

avatar image Tomer-Barkan · Nov 05, 2013 at 09:40 AM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check the latest key pressed? 2 Answers

On Screen Keyboard (PC and Console) best practices 3 Answers

Make GUI appear on key down 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

Paper, Rock, Scissor 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges