0
\$\begingroup\$

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:

screenshot

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?

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

collide.Transform refers to the object you are colliding with. Change this line

Transform resourceTransform = collide.transform

to

Transform resourceTransform = transform

and try again

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.