Count only activated child

Hello Unity :slight_smile: i cant figure out how to get count/sum of all ONLY activated Child2 in multiple gameObjects(clones)
… i have SceneControler to count with Start() method etc… and want to get Debug.Log(sum);
If somebody know solution i can’t figure out, thank you :slight_smile:
and oh btw Child2 tag is star2

gameObject(clone)
ChildMain
Child1(activated)
Child1(activated)
Child2(not-activated)
Child2(not-activated)

I don’t think there is a shortcut method. So you’ll need to loop through the children

int count = 0;
foreach(Transform child in parentObject.transform)
{
    if(child.gameObject.activeSelf && child.gameObject.CompareTag("star2"))
      count++;
}

If you only want certain child, use compareTag to see if it also has the correct tag.

public Transform parentObject;

    void Start () {

        int count = 0;
        foreach(Transform child in parentObject.transform)
        {
            if(child.gameObject.activeSelf && child.gameObject.CompareTag("starss"))
                count++;
         
        }
                      Debug.Log (count);
    }
}

i drag parentObject … that grandChildren and count all but solution is still 0 :\

Well, no, transform is only going to get direct children. If you want grandchildren and you only have one child, you could do

parentObject.transform.GetChild[0].transform

If you have many children, you’ll need to loop through the children and then the grandchildren. Also you are doing compareTag to starss, but you mention star2 in your original post. Make sure you’re looking for the correct tag as well.

But you could also just reference the child if there is only 1. Just depends on your setup. I just gave the info on how you can loop children. You’ll need to adapt it to your setup.

Thank you for information ill try to figure out :wink:

You could LINQ it in a single line if you felt inclined to do so.

You could make a recursive function:

public int activeChildCount(Transform t) {
    int count = 0;
    recursiveAction(t, (Transform child) => {
        count += child.gameObject.activeSelf ? 1 : 0;
    });
    return count;
}

public void recursiveAction(Transform t, UnityAction<Transform> action) {
    action(t); // if you dont want this to apply to the given root transform, remove this
    foreach(Transform child in t) {
        recursiveAction(child, action);
    }
}

But in my opinion recursion is not a very readable way to program something.

I think that’s more to do with your naming conventions and style then with recursive programming in general. :stuck_out_tongue:

It would be pretty easy to make your function more readable.

public int GetActiveChildCount(Transform t) {
    int count = 0;
    foreach (Transform child in t) {
        if (child.gameObject.activeSelf) {
            count++;
            count += GetActiveChildCount(child);
        }
    }
    return count;
}
1 Like

I disagree. Style and naming convention definitely help, but an iterative loop with a clear end condition will almost always be more readable than a recursive loop. I only use recursion when the problem can’t be easily solved with an iterative solution.

Reason being, a recursive function requires the reader to interpret the recursion and determine what it will aggregate and when it will stop (potentially never). An iterative solution is linear logic and easily interpreted, especially with good naming conventions and style.

Plus I was also hesitant to offer a recursive solution to a new Unity developer, which is why I broke the recursion out into a reusable function that can be used in place of iterative loops for any Transform hierarchy loops. :slight_smile:

I’m not however arguing that my original example has good style or naming conventions. I wrote that with haste, and using style conventions that my current job uses, which is akin to javascript.