- Home /
Finding objects - same name - different atributes
Hi again!
Im using GameObject.Find so i can find an object in my scene, but when another object that has the same name appears, this GameObject.Find keeps finding the first object with that name.
they must have the same name for collisions purposes.
Is there a way to find the second object that appears?
Or is there a way to find all objects that has the same name and then manipulate only one using a conditional for one particular atribute?
Once heard about a function called FindAll, it exist? or something similar?
Thank you!
Answer by screenname_taken · Jul 07, 2014 at 09:33 PM
http://answers.unity3d.com/questions/24257/how-do-i-find-all-game-objects-with-the-same-name.html
First thing that pops up in google. Could help i think.
Answer by Loius · Jul 07, 2014 at 09:41 PM
you should avoid relying on object names wherever possible. you never NEED an object's name; it's similar to lazy-typing in Unityscript in that it can make it easier to code but you introduce headaches if you're not very careful.
i would never try to find an object by name if there could be two objects with the same name. you should either keep track of instantiated objects in a script or use public variables to assign references in the inspector.
As for FindAll... http://docs.unity3d.com/ScriptReference/index.html
Answer by elliospizzaman · Jul 07, 2014 at 10:32 PM
It is best not to rely on names unless absolutely necessary. I would personally advise finding gameObjects by either their tag or their type. However here is one way to find all gameObjects in the scene that have a given name:
GameObject[] FindGameObjectsByName(string name){
return System.Array.FindAll ((FindObjectsOfType(typeof(GameObject)) as GameObject[]), p => p.name==name);
}