- Home /
Question by
Hyperactive · Feb 02, 2015 at 12:32 PM ·
parentchildtag
Enable/Disable all children of parent with tag
Hi All !
So I've been looking around for a while now and I haven't managed to find an answer that works to what I'd like to achieve.
I'm looking for a way to find all gameobjects with a tag with GameObject.FindGameObjectWithTag and then disable all of the direct children under that parent, without disabling the parent.
Something like this ?
var fires : GameObject[] = GameObject.FindGameObjectsWithTag("fire");
for(var aFire : GameObject in fires)
{
aFire.active = false;
}
Thanks !
A
Comment
Best Answer
Answer by Chris333 · Feb 02, 2015 at 01:17 PM
Hi,
try something like that, sry but its C#:
GameObject[] fires = GameObject.FindGameObjectsWithTag("fire");
for(int i=0; i<fires.Length; i++)
{
for(int j=0; j<fires[i].transform.childCount; j++)
{
fires[i].transform.GetChild(j).gameObject.active = false;
}
}
But keep in mind that this is not very performant. E.g. you shouldn't use it in the Update() method.
Hi ! Thanks for the answer. I'll try this tonight when I'm with my project :)
Absolutely fantastic ! It works like a charm. Thanks a lot !