- Home /
getting children of the parent but excluding objects with certain tag
hi guys, i needs some help with getting children of the parent but excluding the objects with certain tag, here is the code i used so far:
//GET CURRENT CAMERA SELECTION selection=cam.currentTarget;
//GET THE PARENT OF THAT OBJECT selectionParent=selection.parent.transform;
//THIS WILL GET ALL THE CHILDREN OF THE PARENT
children=Array(selectionParent.GetComponentsInChildren(Transform));
this code will return all children of the parent no matter the tag, so how to include the checking of the tag into this. for example if the tag is "exclude" how to get the children of the parent but only with other or no tag that is "exclude"
thanks!
Answer by Mike 3 · Nov 11, 2010 at 03:35 PM
Something like this:
children = new Array();
for(var child : Transform in selectionParent.GetComponentsInChildren(Transform))
{
if (child.tag != "exclude")
children.Add(child);
}
Answer by Binbag42 · Feb 11, 2017 at 02:09 PM
Another option is to use transform.GetChild() see GetComponentsInChildren - not parent and children. Restriction: It works if all the children have the component you are looking for so it works for Transform.
Your answer