• 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 gotanidea · May 13, 2012 at 07:31 AM · androidjavascripttouchaugmented realityqualcomm

2 Buttons on Android

Hello, I have 2 virtual buttons on my Android phone screen. Suppose the names are button A and button B.

What I want is: When I touch button A, only function A should be executed. And when I touch button B, only function B should be executed.

The problem is: When I touched button A, both function A and B were executed. Same thing was happened when I touched button B.

My code:

  • Button_A.js (attached to Button A):

    private var latched = false;

    function Update () { if (Input.touchCount > 0) { var touch = Input.GetTouch(0);

            if(touch.phase == TouchPhase.Stationary)
             {
                 latched = true;
             }
             
             if(touch.phase == TouchPhase.Ended)
             {
                 latched = false;
             }
         }
    
         if (latched)
         {
             Function_A();
         }
     }
     
    
  • Button_B.js (attached to Button B):

    (similar to ButtonA.js, but this executes Function B)

What is wrong with my code?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rutter · May 13, 2012 at 07:36 AM

It looks like you're checking whether the user is touching, but not where (or what) they are touching. As a result, both buttons see that the user is touching, and both functions are called.

I assume these buttons are objects in your scene? You can use the screen position of the user's touch to send a raycast out from the camera to identify the collider the user is effectively touching.

You can probably find quite a few tutorials on doing exactly this. Once you've resolved a screen position, it's roughly the same problem as "what did the user click on?", which is how most people phrase the question. Most likely you'll be using Camera.ScreenPointToRay() and Physics.Raycast().

Comment
Add comment · Show 1 · 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 gotanidea · May 14, 2012 at 05:51 AM 0
Share

Thanks. Yes, the buttons are GameObjects, not GUITextures. Sorry for the late reply. Could you show me how to use the screen position to identify the collider the user is touching? Or can you give me links to the tutorials?

avatar image
2

Answer by syclamoth · May 13, 2012 at 07:35 AM

Problem is- neither of these functions check the position of the touch! You need to add something in the script that checks a position on the screen- try adding something like this:

 // at the top
 public var buttonPos : Rect; // This is the position of the button on the screen

 ... in the Update part-

 if(buttonPos.Contains(touch.position))
 {
     // do stuff
 }
Comment
Add comment · Show 3 · 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 gotanidea · May 14, 2012 at 05:44 AM 0
Share

Thanks, you're right. Sorry for the late reply. How can I know the right x, y, w, & h for the Rect?

avatar image syclamoth · May 14, 2012 at 06:11 AM 1
Share

You just have to know? I can't tell you the exact dimensions of the button you want, that's like telling me to draw you without having ever seen your face!

avatar image gotanidea · May 14, 2012 at 06:19 AM 0
Share

Suppose I draw a Rect with Rect(0, 0, 10, 10). How can I view it on the Unity editor Game simulation window? Because it doesn't have color

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Scale / Scaling Object using touch on Android 0 Answers

raycasting and layermask question 2 Answers

Unity touch 1 Answer

Disable gui texture in a different way? 0 Answers

Android touch question 0 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