Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check child count for the active child objects

I have a parent game object that has 20 child objects. Few of them are active and rest are inactive. How do I check my if statement for the 5 and less active child objects?
Currently it is just counting the child count irrespective active/inactive state of them.

void Start () {

        //Active Child count

        if (this.transform.childCount <= 5) {

        }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you are into inq you could be unig e.g.

using System.Linq;

...

var count = transform.Cast<Transform>().Where(child => child.gameObject.activeSelf).Coun();

For whatever reason Transform only implements the generic IEnumerable so you first have to Cast the results from object into Transform again. Then you filter only those of active GameObjects and finally you Count those.


Or just use the explicit form

var count = 0
foreach(Transform child in transform)
{
    if(child.gameObject.activeSelf) count++;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading