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 galaboy · Jun 27, 2015 at 06:44 AM · scripting problemaiscriptingbasics

identifying the index of a gameobject in an array

hi, am doing a tic tac toe game, i need help on how to identify the index of a gameobject in an array when the object is clicked. am trying to figure it out but i couldn't. also if you could please explain me how it works.

Comment
Add comment · Show 2
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 Denvery · Jun 27, 2015 at 06:45 AM 0
Share

Hello! Please tell what array do you mean?

avatar image Denvery · Jun 27, 2015 at 07:15 AM 0
Share

So, if you store objects in array, you can detect which object was clicked, then, for example, write (in C#): objectsArray.IndexOf(clickedObject)

1 Reply

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

Answer by Addyarb · Jun 27, 2015 at 07:30 AM

Hi there!

Sorry to hear you're having trouble. There are quite a few different ways to handle this, but I'll try to give you the easiest.

Lets create an X and O Manager that holds the array. To do this, follow these steps:

  1. Create a new gameObject and name it "Manager"

  2. Create a new script called "TicTacToe_Manager" and attach it to the gameObject.

  3. Create a tag called "XO"

  4. Click each of your X or O gameObjects (or their prefabs) and tag them with "XO" tag.

  5. Double click the script and put something like this in it.

      //This is the top of the script underneath the class name
         public GameObject[] myArray; //We make the array public so that we can reference it from other scripts.
         
         
         void Start(){
         myArray = GameObject.FindGameObjectsWithTag("XO");
         //For each of the gameObjects with the tag "XO" in the scene, add it to the array.
         }
         
    
    
    

Next,

  1. Create a script called "TicTacToe_Object"

  2. Attach it to each of your X or O objects (or the object prefab).

  3. Double click the script and type in the script below:

      //Top of script underneath class name:
         
         TicTacToe_Manager XO_Manager;
         
         void Start(){
         XO_Manager = GameObject.Find("Manager").GetComponent<TicTacToe_Manager>();
         }
         
         void OnMouseDown(){
         //if this object is clicked by the mouse...
         for(int i = 0;i<XO_Manager.myArray.Length;i++){ //for each object in the array..
              if(XO_Manager.myArray[i].transform == transform)[ //if that array object's transform is also *this* transform...
                        Debug.Log("You just clicked the object with the Index of " + i.ToString(); //Send a message to the console with the ID we just clicked.
                   }
              }
         }
    
    
    

Good luck! And let me know if you have any questions.

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 galaboy · Jun 27, 2015 at 08:25 AM 0
Share

thanks for the explanation addyarb, previously i was using separate gameobject to detect the mouse click and to assign the X or O marks on the collider, but now i got a new idea of using arrays to detect the index of the object that is clicked. i will get back with the status of my work as soon as i get it done. thanks once again for ur valuable effort to help me. thanks again for ur help.

can u tell me why do we use .ToString();

avatar image Addyarb · Jun 27, 2015 at 06:16 PM 0
Share

You're very welcome!

.ToString() is a way to convert a non-string variable to a string. In the Debug.Log it's not usually necessary to use it, but if you are sending that int to a text component, it is necessary to convert an integer to a string. However - as I said - Debug.Log has a way of implicitly (can do it on it's own) way of converting integers to strings.

Hope it all works for you!

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

23 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

need help in script 1 Answer

OnTriggerEnter called only one time?!! 5 Answers

All my scripts have suddenly stopped working? 0 Answers

Changing 2 dependent variable at the same time 1 Answer

How would I set up this blend tree for my 2d game 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