- Home /
How can I select 3 different groups of objects with the same script?
I've got 3 lights as child inside 3 different groups. Each of these lights has the same script. And the script determines the behaviour of the lights, which must be the same.
I want to activate the lights of each group depending on certain conditions.
The solution could become from some ways, but one of them is: I'm thinking about re-tag them, with different tags depending on the group the lights belong to. Should I create a vector variable of three elements and fill it in some way with the objects tagged with a certain tag defined by me and then apply a for loop for selecting them?.
var luces_respirar : GameObject[];
luces_respirar = GameObject.FindGameObjectsWithTag("luz_respirar");
if(Network.isServer)
{
if ( (luces_resp_encendidas==true)) {
for (var GameObject in luces_respirar)
{
active=true;
}
}
}
I can't add a schematic picture to show exactly what i'm trying to do...I don't know how to do this, sorry.
Answer by kalvinlyle · Apr 04, 2012 at 09:09 AM
You can use this script to perform actions on all the children of an object:
for (var child : Transform in transform) {
child.active = true;
}
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Or you can use SetActiveRecursively(true) on the parent objects for each set of lights.
I have achieved introduce my objects in a vector, so I just only have to activate all of them. I think I'm not doing well on my way. Thanks for your answer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065858im_/https://answers.unity.com/themes/thub/images/avi.jpg)