identify arrayed game objects
Hi, Programmer new to Unity. I have instantiated game objects to a multi-dimensional array: instantspheres[z1, x1] = (GameObject)Instantiate(spheregreen1, spawnPosition, spawnRotation);
spheregreen1 prefab has a collider with Is Trigger checked, and a script attached, so I click and OnMouseDown event triggers in script. In the script event, how can I determine which object of the array was clicked, ie, what is z1,x1 of this object?
looks like the only way I can figure is to assign a tag when array game object is instantiated. tags have to be created already (edit/project settings). then you can use the On$$anonymous$$ouseDown event, which has gameObject as a ref to the collider's game object, then extract tag with gameObject.tag.
if anyone has any better technique, great!
Answer by tom_carr_2 · Dec 30, 2015 at 08:55 PM
Thanks for taking the time.
I'm using a multi-dim. array because I'm creating a matrix, say 3x3 game objects, so easier to manipulate in code. The way I did it is to step thru nested for loops (z and x) to identify the object and then take actions.
You said it would be better to make objects with a tag. Then a controller script would be directly accessed with a click and actions could be taken. Why do you say this would be a better way? Thanks again.
I didn't say anything about tags :)
I'm not sure I'm following how putting game objects in a 3x3 array makes them easier to manipulate in code, unless you're manipulating them based on those locations (which is backwards, I feel).
Let's say you are doing a rubix cube example, which is a 3x3 matrix (9 total blocks). If you are looking at one face of the cube and click on the top-left block, you wouldn't attempt to reference that block based on '1, 1', you would already have reference to that block by clicking on it. If you wanted to see that it is next to '1, 2' and '2, 1', etc, you would best use variables stored on the instance (x, y, z) to indicate the index of that array
thanks for your input. I appreciate you taking the time. and I don't know the best way, like any program$$anonymous$$g task, I just came up with a solution.
sorry for bad info: when I'm talking about 3d locations, not x,y,z but like your rubix cube, the location is relative comparison- like color on two squares of rubix cube.
to provide clarity, following the rubix cube example, if the colors are randomly assigned, then when 1,1 is assigned, it does not know what the others will be. I was not talking about a manipulation (my error) but an analysis to deter$$anonymous$$e what the rest of the colors are after the random assignment is done. then when 1,1 is clicked, I use the array to analyze what adjacent colors are.
I felt the design option was to used fixed objects and change the material or instantiate prefabs. I made a random array, then instantiated objects based on the array. the problem then is when I click an object I have to figure out which one I clicked so I can use array to compare. so I added tags when instantiated- the only solution I could think of.
Co$$anonymous$$g up with a solution is good :) If I were doing something like this, I think I would store their locations on the individual objects as well as the objects in an array. That gives you direct access to their location based on the one you click on so you can identify them in the array.
Answer by A_Li_N · Dec 30, 2015 at 05:43 AM
Why are you using a multi-dimensional array? And why do you need to track the fact that GameObjects are in the locations they are in the array?
If you are attempting to get the location of the clicked GameObject, it would be better to store that location (z1, x1) on the GameObject itself (or, rather, a script that has those properties), then use Unity to figure out what GameObject you clicked on and reference its locations.
Beyond that, there's Dictionary (using System.Collections.Generic) or looping over the array until you find the one you're looking for; neither option will be overly great compared to flipping things around.
(Let's hope I didn't completely mis-understand what you were asking) :)
Your answer
Follow this Question
Related Questions
checking if gameobject exists 1 Answer
CHANGING SCENE AND MAKE A GAME OBJECT ACTIVE THERE 0 Answers
Spawn objects in a row with same spacing 0 Answers
How to change the shape of gameObjects 2 Answers
Accessing values on a gameobject 1 Answer