- Home /
Question by
TheDawningLegend · Jul 03, 2016 at 06:56 PM ·
c#childloopchildren
Loop to find a child in every children's child?
I am looking to make a function that loops to find a child (can be a child of a child and so on). Currently, the loop only looks at the children of the game object, but not their own...
Transform Holder (string holderName) {
foreach (Transform child in transform)
if (child.name == holderName + " holder")
return child;
return null;
}
Notice : This is to loop in a list of bones, so the amount of children and sub-children is unknown. Thanks for the help!
Comment
I believe you would need to fun another foreach inside checking for children also so a function inside you your foreach loop that check if there is children then looks for its children this would need to be repeated enough times to get every child if you explain more to why you want to do this you may get a better way to do this
Best Answer
Answer by Fire_Cube · Jul 03, 2016 at 07:28 PM
Transform Holder (Transform where, string holderName) {
foreach (Transform child in where)
if (child.name == holderName + " holder")
return child;
else
{
Transform tr = Holder(child, holderName);
if(tr != null)return tr;
}
return null;
}
That should work :)