Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 thereal_mpellegr · Nov 07, 2013 at 08:44 PM · inputbuttontouchclick

Input.getTouch() vs checking if button is clicked

In my application, I create buttons like this:

 if(GUILayout.Button(GUIContent(menuButton.text), menuButton.getGuiStyle())){
     menuButton.executeClick();
 }

So whenever the button is clicked the executeClick() function is called on it. Now I also have a window behind the buttons that accepts user input and queries on every Update() with

 if(Input.touchCount > 0){
     var touch : Touch = Input.GetTouch(0);
 
     //use the touch...

When I click a button, the code used to interpret the touch event is executing when I really only want the menu button code to in executeClick(). What's a good way that I can first check if a button is clicked and if none are to use the touch information?

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 thereal_mpellegr · Nov 07, 2013 at 09:26 PM 0
Share

Since it appears that the first section of code is executed before the update function for my window, I put a global boolean that I set to true when the first section is executed, and I check that before allowing the second code to execute. Am I making a bad assumption that the first is always called before the second?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by chaosmaker · Nov 07, 2013 at 09:46 PM

Am I making a bad assumption that the first is always called before the second?

Yes, it is a bad assumption. Execution order is documented here:

http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html

Also consider here it is stated that:

OnGUI: Called multiple times per frame in response to GUI events. The Layout and Repaint events are processed first, followed by a Layout and keyboard/mouse event for each input event.

Why don't you just put your second logic inside first logic:

 if(GUILayout.Button(GUIContent(menuButton.text), menuButton.getGuiStyle())){
             menuButton.executeClick();
     
     if(Input.touchCount > 0){
         var touch : Touch = Input.GetTouch(0);
      
         //use the touch...
 }

Or inside executeClick that you can be sure that touch check is made inside(after) the click.

Or else you have to be more clear about what you are trying to achieve that I could provide a more precise help.

Comment
Add comment · 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

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

16 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

Related Questions

Detect UI Button Click Event in Update method 3 Answers

Button.onClick.AddListener 1 Answer

GUITexture OnMouseDown Problem 1 Answer

Force touch to only work on a new touch - buttons 0 Answers

Help with a button 1 Answer


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