- Home /
How do I know where a randomly selected object is in an array?
I've set up a 2d array of gameobjects[x,y]
The player selects one of these objects in the game world eg. raycast/mouseclick
How do I know which x,y position in the array the selected object is?
I need to know as I have a second array I want to cross-reference.
please halp! ;)
Answer by asafsitner · Dec 23, 2012 at 11:17 PM
Unfortunately there's no built-in method for 2D arrays like IndexOf
.
What you can do is basically 2 things:
Which I personally favorite, is to store on each game object it's position in the array as you instantiate it.
Which is less favorable, is to iterate over the array until you find the object the player has clicked like so.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085842im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Fire Points 2 Answers
Question on for loop with array 0 Answers
Storing transforms from objects in Array 1 Answer
2D Array and other Script method access 1 Answer
Array of Tilemaps misbehaving 1 Answer