# Does Transform.childCount not work correctly?

This is my code:

void OnTriggerEnter(Collider collide) {
if (collide.gameObject.transform.tag == "Laser") {
Debug.Log ("Inside If");
Transform resourceTransform = collide.transform;
Transform[] tf = new Transform[resourceTransform.childCount];
for(int i = 0; i<= resourceTransform.childCount-1; i++){
tf[i] = resourceTransform.GetChild(i);
}

Debug.Log ("Child Count Before: " + resourceTransform.childCount);
resourceTransform.transform.DetachChildren();

Debug.Log ("Child Count After: " + resourceTransform.childCount);
Vector3 centerOfMass = resourceRb.centerOfMass;
Explode (tf, centerOfMass);
}
}


Everything is working fine, except the childCount property is always zero and that screws everything else up. Here's an image of the hierarchy:

The script is on cube1 and the object has 12 or so children. So why is childCount equal to zero? Is it a problem with Unity itself?

• You seem to be getting the child count of the laser. – Ben Jul 19 '15 at 13:56

Transform resourceTransform = collide.transform

Transform resourceTransform = transform