- Home /
GetComponent of ALL clones?
How do I get the component of all the clones of my object?
When I use
box = GameObject.Find("Box").GetComponents();
It only gets the component of the first box he somewhat finds, but ignores the other clones of the boxes.
I also couldn't just use a static variable, since I have another script that only needs to get one component, and when I get the component of the script, its static variables doesn't work for some reason.
Thanks! Simple question, but couldn't find it at all in Google or in Unity Answers.
Have you tried changing the name of your cloned objects from Box(Clone) to Box. You can do this immediately after the instantiate function.
Answer by Tanshaydar · May 23, 2014 at 10:49 PM
You can only get specific type of components in your scene. You can get all AudioSources or Colliders: https://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponents.html
A workaround for your situation might be as follows. Make a tag specific to that object group and tag them all the same. Then create a GameObject array, and then assign all game objects to this array using
yourObjects = GameObject.FindGameObjectsWithTag ("Your Tag");
Then you can get each of their component like this:
yourObjects[0].GetComponent<YourComponent>();
Answer by Jeff-Kesselman · May 23, 2014 at 10:54 PM
Use FIndObjectsOfType
This and many other wonder can be found in the docs... http://docs.unity3d.com/Documentation/ScriptReference/Object.FindObjectsOfType.html