Ad

Destroy A GameObject When It Enters A Trigger Collider?

- 1 answer

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 follows:

#pragma strict

function OnTriggerEnter (other : Collider) {
    Destroy(other.gameObject);
}

What I want the script to do is destroy any object that enters the trigger collider. My game is 2D, if that helps.

Any help is appreciated! :)

Also, what does #pragma strict do and why is it there?

Ad

Answer

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! :)

Ad
source: stackoverflow.com
Ad