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 primus88 · Sep 08, 2013 at 03:55 PM · referencenguicondition

Script detecting which object was clicked

Hello guys,

I have this problem. In my scene I have 20 objects, each with a script with a separate name that basically does the same thing. What it does, is that when the object is clicked, the script attached to it draws with NGUI a certain button.

The problem is that it draws the button for any clicked object, instead of the object which passes the conditions.

The script :

 public class BattleCheck1 : MonoBehaviour {
     
     public GameObject labelAttack;
     public GameObject labelChallenge;
     private int attack1;
     private int challenge1;
 
     // Use this for initialization
     void Start () {
     
         NGUITools.SetActive(labelAttack.gameObject,false);
         NGUITools.SetActive(labelChallenge.gameObject,false);
         
         attack1 = PlayerPrefs.GetInt("attack1");
         challenge1 = PlayerPrefs.GetInt("challenge1");
         
     }
     
     // Update is called once per frame
     void Update () {
         
         if(MapMenu.checkChallenge == true)
         {
             if(attack1 == 1)
             {
                 NGUITools.SetActive(labelAttack.gameObject,true);    
             }
             
             if(challenge1 == 1)
             {
                 NGUITools.SetActive(labelChallenge.gameObject,true);    
             }    
         }
     
     }

The other objects have the scripts with variables attack2,3,4.. and challenge2,3,4 etc. For now I have only the attack1 = 1 and the rest of the variables are set to 0, but the button is drawn each time I click on any object.

How can I fix this?

Thank you

Comment
Add comment · Show 6
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 DeveshPandey · Sep 08, 2013 at 04:04 PM 0
Share

can I see your full code, actually I am unable to getting you exactly.

avatar image primus88 · Sep 08, 2013 at 04:09 PM 0
Share

This is the code that does it.

I have this NGUI button.. and I want it to be drawn or not, based on which objects I clicked (based on the attack[n] variable). Right now it doesn't matter if attack1=1 and attack2=0 if I click on the second object it will still show me the button, even though it shouldn't.

avatar image primus88 · Sep 08, 2013 at 05:24 PM 0
Share

Should I explain this better ? Can anyone let me know please ?

avatar image ArkaneX · Sep 08, 2013 at 08:43 PM 0
Share

Actually seeing the full code (especially the part related to handling click) would be good.

As a side note - do you have 20 scripts? BattleCheck1 - BattleCheck20, with different variable names but the same code? If yes, then this is not the way to do it. I don't know how your game should work, but I strongly believe it is possible to use one script (maybe slightly modified) for all the objects.

avatar image ArkaneX · Sep 08, 2013 at 09:32 PM 1
Share

I still don't get how it all works, but you should be able to get one script only by changing it to:

 public class BattleCheck : $$anonymous$$onoBehaviour {
  
     public int attack;
     public int challenge;
     public int number;
 
     private CommonCrap zaScript;
  
     // Use this for initialization
     void Start ()
     {
        attack = PlayerPrefs.GetInt("attack" + number);
        challenge = PlayerPrefs.GetInt("challenge" + number);
 
        GameObject theCamera = GameObject.Find("$$anonymous$$apCamera");
        zaScript = theCamera.GetComponent<CommonCrap>();
     }
 
     void Update()
     {
        if ($$anonymous$$ap$$anonymous$$enu.checkChallenge == false)
        {
          NGUITools.SetActive(zaScript.labelAttack.gameObject,false);  
          NGUITools.SetActive(zaScript.labelChallenge.gameObject,false);
        } 
     }
  
     void On$$anonymous$$ouseUp()
     {
        GameObject theCamera = GameObject.Find("$$anonymous$$apCamera");
        CommonCrap zaScript = theCamera.GetComponent<CommonCrap>();
          if(attack == 1)
          {
           NGUITools.SetActive(zaScript.labelAttack.gameObject,true);  
          }
  
          if(challenge == 1)
          {
           NGUITools.SetActive(zaScript.labelChallenge.gameObject,true);   
          }    
     }
 }

and then, after dragging your script to the game objects, you have to set their numbers from 1 to 20. This is of course a quick and probably not best solution, but much better that the one you're using.

Show more comments

1 Reply

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

Answer by darthbator · Sep 10, 2013 at 12:42 AM

Why not raycast from the camera at the mouse cursor position and then gather information based on what the raycast hit? That's how I handle all this manner of stuff. That way you can switch off data already on that gameObject (tags, layers, gameObject.name, or any attached script).

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How do I call a function in another gameObject's script? 5 Answers

Explanation of slerp example in reference 1 Answer

Call Functions Across Scripts, Null Object Error 1 Answer

Script works for NPCs, but not Player 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