• 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 StartUpStudios · Sep 25, 2014 at 06:22 PM · androidjavascript

Detecting tag on Android Hittest.

The code Im using is working fine, but I have to assign the script to every GUItexture I want to use as a button. I'm overhauling my entire menu system, so I want to simplify everything by just attaching this script once to an empty gameobject, then on hittest, look for tag, then run if statements to direct the flow from there. Such as

 if(****.tag == "Chapter1"){
 *Go to chapter 1 level select*
 }
 if(*****.tag == "Chapter 2"){ etc...

But I don't know how to detect tag on hittest. If it's even possible. Here is the code Im using now.

 #pragma strict
 
 var startButton : GUITexture;
 public var deleteSave : boolean = false;
 public var Chapter : int;
 
 function Start () {
 startButton.color.a = 0.0f;
 yield WaitForSeconds(2);
         yield Fade(0.0, 1.0, 2.0);     // fade up
       //  yield Fade(1.0, 0.0, 2.0);     // fade down
     }
 
 function Update () 
 {
 AutoResize(Screen.width, Screen.height);
 if(deleteSave==true){ 
 PlayerPrefs.DeleteAll();
 deleteSave=false;
 }
        for (var i = 0; i < Input.touchCount; ++i)
        {
             if(startButton.HitTest(Input.GetTouch(i).position)){
                    if (Input.GetTouch(i).phase == TouchPhase.Began) 
                {
                         if(Input.GetTouch(i).tapCount == 1)
                         {
                         if(Chapter==1){
                        
                           }
                  
                           
                         }
                }
        }
        }
        
       
 }
 
 public static function AutoResize(screenWidth:int, screenHeight:int):void
 {
     var resizeRatio:Vector2 = Vector2(Screen.width / parseFloat(screenWidth), Screen.height / parseFloat(screenHeight));
     GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(resizeRatio.x, resizeRatio.y, 1.0));
 }
      
     
      
     function Fade (startLevel :float, endLevel :float, duration :float) {
         var speed : float = 0.5/duration;  
         for (var t :float = 0.0; t < 1.0; t += Time.deltaTime*speed) {
             startButton.color.a = Mathf.Lerp(startLevel, endLevel, t);
             yield;
         }
     }

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

1 Reply

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

Answer by robertbu · Sep 25, 2014 at 06:30 PM

In order to rewrite the code the way you describe, you will need an array of guiTexture object to test. You can create that list by dragging and dropping into a public array, or you can tag them all the same and use GameObject.FindGameObjectsWithTag(). So say your array is called 'uiElements'. If the elements are GUITextures, your code will have:

 Touch t = Input.GetTouch(i);
 if (t.phase == TouchPhaseBegin && uiElemens[i].HitTest(t.position) {
     Debug.Log("You hit " + uiElements[i].name;
 }

If you collected them using FindGameObjectsWithTag(), you will have an array of game object, and your test might look like:

 Touch t = Input.GetTouch(i);
 if (t.phase == TouchPhaseBegin && uiElemens[i]guiTexture.HitTest(t.position) {
     Debug.Log("You hit " + uiElements[i].name;
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

[Android]How to move an object and have it snap to grid? 1 Answer

TPS Camera with Joystick (Android) 0 Answers

Destroy GameObject A or B 1 Answer

Cube terrain with perlin noise 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