Skip to content Skip to sidebar Skip to footer

Destroy A Gameobject When It Enters A Trigger Collider?

I am having trouble destroying a GameObject when it enters a GameObject with a BoxCollider2D that is set to be a trigger. I have a javascript script on the GameObject that reads as

Solution 1:

I fixed the issue in a way. The 2D collider is off screen, so I unchecked the 'Is Trigger' checkbox and used this code instead:

function OnCollisionEnter2D(coll: Collision2D) {
    if (coll.gameObject.tag == "toast")
        Destroy(coll.gameObject);
}

Now my toast sprites get destroyed when they hit the collider.

Hope this helped! :)

Post a Comment for "Destroy A Gameobject When It Enters A Trigger Collider?"