• 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 Rick.Miranda · Oct 03, 2013 at 03:24 AM · mobilescriptingproblemtouches

Error when using GUITexture.HitTest

Hi

I'm trying to use GUI.Texture.HitTest(touch.position) to get the true or false bool when user touches the mobile screen. I get the following error message: "BCE0019: 'Hit Test' is not a member of UnityEngine.GameObject."

Here's my code:

    var GUI_LeftArrow : GameObject;
     var GUI_RightArrow : GameObject;
     
     function Awake() {
         GUI_LeftArrow = GameObject.Find("GUI_LeftArrow");
         GUI_RightArrow = GameObject.Find("GUI_RightArrow");
     }
     
     function Update() {
         for (var touch : Touch in Input.touches) {
             if (touch.phase == TouchPhase.Stationary && GUI_LeftArrow.HitTest(touch.position)) {
             Debug.Log("Left Arrow Down");
             }
         
         }
     }

GameObject.Find is getting the GUITexture (Game Object) from the hierarchy panel in Unity. Maybe I'm missing an import into the script at the top? Any help will be great.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Oct 03, 2013 at 03:44 AM

Unity is right! HitTest is member of GUITexture, not of GameObject - you should use the guiTexture property of GUI_LeftArrow instead:

 if (... && GUI_LeftArrow.guiTexture.HitTest(touch.position)){

NOTE: never use Find inside Awake - the objects you're looking for may have not been created yet. Rename the function Awake as Start and potential headaches will be avoided.

Comment
Add comment · Show 2 · 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 r-dotfunky · Oct 03, 2013 at 03:57 AM 0
Share

Removed my incorrect answer - apologies!

avatar image tw1st3d · Oct 03, 2013 at 04:08 AM 0
Share

Good tip on the Start ins$$anonymous$$d of Awake usage.

avatar image
0

Answer by TrickyHandz · Oct 03, 2013 at 04:02 AM

GameObject doesn't have a HitTest...but GUITexture does. So, you need to declare your variables a GUITexture not GameObject. Then you need to get the guiTexture property of the GameObject found in the Start Function as a GUITexture. Give this a try:

 var GUI_LeftArrow : GUITexture;
 var GUI_RightArrow : GUITexture;
  
 function Start() {
     GUI_LeftArrow = GameObject.Find("GUI_LeftArrow").guiTexture;
     GUI_RightArrow = GameObject.Find("GUI_RightArrow").guiTexture;
 }
  
 function Update() 
 {
     for (var touch : Touch in Input.touches) 
     {
        if (touch.phase == TouchPhase.Stationary && GUI_LeftArrow.HitTest(touch.position)) 
        {
            Debug.Log("Left Arrow Down");
        }
  
     }
 }
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 Rick.Miranda · Oct 03, 2013 at 10:35 PM 0
Share

Thank you so much TrickyHandz. This is the solution. Appreciate your help. :-)

avatar image TrickyHandz · Oct 03, 2013 at 10:37 PM 0
Share

No problem. Just mark the answer as accepted when you have a chance. Happy coding!

avatar image Rick.Miranda · Oct 03, 2013 at 10:42 PM 1
Share

Sure, I'd love to do that. Just not sure where or if I have to have reputation points to be able to upvote or mark as accepted...let me know.

avatar image
0

Answer by Rick.Miranda · Oct 03, 2013 at 10:41 PM

Final Working Solution for detecting GUITexture touch events:

 var GUI_LeftArrow : GUITexture;
 var GUI_RightArrow : GUITexture;
 
 function Start() {
     GUI_LeftArrow = GameObject.Find("GUI_LeftArrow").guiTexture;
     GUI_RightArrow = GameObject.Find("GUI_RightArrow").guiTexture;
 }
 
 function Update() {
     for (var touch : Touch in Input.touches) {
         if (touch.phase == TouchPhase.Stationary && GUI_LeftArrow.HitTest(touch.position)) {
         //if (touch.phase == TouchPhase.Stationary) {
         //if (GUI_LeftArrow.HitTest(touch.position)) {
         Debug.Log("Left Arrow Down");
         }
     
     }
 }
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.

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

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

MouseUp not working on Mobile build 0 Answers

FPS touch bug in Asset Sample Beta? 0 Answers

Mobile Input Not Accurate 0 Answers

Touch not working on guiTexture. 1 Answer

Moving an object in Unity3d for Android 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