Deep childCount Possible?

I’m trying to get the number of children and children of children which I guess would be called the deep child. I’ve tried using Transform.childCount, but it only gives me the children, not the children of those children. Transform.IsChildOf seems capable of finding the deep children, but I’m not sure if I can implement this for my purposes. I’d appreciate any help. Thanks!

I don’t know of a way built in to do this but you could do it yourself. If your using C# try slipping this into any file

public static class TransformExtenderUtil { 
    public static int CountDecendants( this Transform transform ) {
        int childCount = transform.childCount;// direct child count.
        foreach(Transform child in transform) {
            childCount += CountDecendants(child);// add child direct children count.
        }
        return childCount;
    }
}

its recursive but it shouldn’t be too big of a issue, though i’d use it sparingly.

And its a extension method so to call it you can do

transform.CountDecendants();

I’m using JS since I don’t really know much about C#, but here’s what I have so far from trying to convert it. I am still getting errors though, but I’m sure it’s just because I converted some stuff wrong. So, if someone could help me fix what I have, it’d be greatly appreciated! Here’s what I have:

function CountDecendants( Transform ) : int
{
	var childCount : int = transform.childCount;// direct child count.
    for(var child : Transform in transform) 
    {
		childCount += CountDecendants(child);// add child direct children count.
    }
    if(childCount >= 2)
    	Destroy(gameObject);
    return childCount;
}

Hey, I think it can help you. it’s a package that return all the children and children of children. Sorry it’s in C#.

522985–18523–$childCount_package.unitypackage (5.03 KB)

unfortunatly i dont know js very well but i think the issue here is the parameter and how your not using it. I think it would be:

static function CountDecendants( transformToCount : Transform ) : int
{
	var childCount : int = transformToCount .childCount;// direct child count.
    for(var child : Transform in transformToCount ) 
    {
		childCount += CountDecendants(child);// add child direct children count.
    }
    return childCount;
}

then you would call that like CountDecendants( this.transform )

I marked it as static because you should not use the local transform var/property as its recursive.
So to explain how that works is

  • You give it a transform for its parameter
  • It initializes a int variable to the .childCount of the given transform
  • For each child it follows all these bullet points including the next one and adds it to the previous callers int
  • The sum is returned

So if you have 3 children which have 2 children each its going to end up calling that function 7 times ( once for given initial transform and then for each child of that transform and then for each child of child and so on and so on )

The reason i’m mentioning this is because the if(childCount > 2) you had in there to destroy the object it could do that in any child, and it might mess up the recursiveness of the function because its in a foreach loop. So avoid deleteing anything inside something that could be recursive like this and do it outside of the initial function call.

Thanks for all the help, I decided to just use it as C# and have my other scripts in JS. That part seems to work great now! Hopefully, I won’t run into any more issues in the near future.