Search code examples
c#unity-game-engineparent-childunityscriptdestroy

Destroy Parent Object Unity3D


I'm facing the problem that I wish to destroy a parent object but I couldn't do so.

I wish to destroy User01 while bullet hit any of these child.

My code was as below.

JS:

function OnControllerColliderHit(col:ControllerColliderHit) {
    if(col.gameObject.tag == "Bullet") {
        Destroy(col.transform.parent.gameObject);
    }
}

C#:

public class PlayerDoe : MonoBehaviour {

    private GameObject par;

    // Use this for initialization
    void Start() {
        par = transform.parent.gameObject;
    }

    void OnCollisionHit(Collision col) {
        if (col.gameObject.tag == "Bullet") {
            Destroy(par);
        }
    }
}

Solution

  • I believe you need to use OnCollisionEnter, instead of OnCollisionHit.