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 :
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++;
}