- Home /
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.
So, if you store objects in array, you can detect which object was clicked, then, for example, write (in C#): objectsArray.IndexOf(clickedObject)
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:
Create a new gameObject and name it "Manager"
Create a new script called "TicTacToe_Manager" and attach it to the gameObject.
Create a tag called "XO"
Click each of your X or O gameObjects (or their prefabs) and tag them with "XO" tag.
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,
Create a script called "TicTacToe_Object"
Attach it to each of your X or O objects (or the object prefab).
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.
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();
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
![](https://koobas.hobune.stream/wayback/20220613201441im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
need help in script 1 Answer
OnTriggerEnter called only one time?!! 5 Answers
All my scripts have suddenly stopped working? 0 Answers