Hi,
I’m going around in circles attempting to solve a fairly simply problem.
I have an object with several children. I want to find which of those children have children (if any) and then parent another object to the first child without a child.
So for example.
Sphere
TargetObj
-Child1
-Child2
-Child3
So sphere object should parent to the first available child of TargetObj that doesn’t already have a child.
At the moment, I have this:
void ChildArray()
{
Target = new Transform[transform.childCount]; //Array of children
for (int i = 0; i < transform.childCount; i++)
{
Target[i] = transform.GetChild(i);
}
foreach (Transform child in Target)
{
if (child.transform.childCount == 0)
{
Debug.Log(child);
newTarget = child;
Sphere.transform.parent = newTarget;
}
The above kind of works, the debug.log(child) shows me that Child1, Child2, Child3 all have no children (great!). Unfortunately when I parent the sphere to the ‘child’ the sphere always defaults to the last child object (child3), I assume because it’s the last object in the hierarchy. But I want the sphere to parent to child1, or whichever is the first object that doesn’t already have a child. I’ve gone around in circles trying to make that happen, I feel like I’m missing a step.
Any help appreciated.