Destroy a GameObject when it enters a trigger collider?

岁酱吖の 提交于 2019-12-12 04:17:18

问题


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?


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



来源:https://stackoverflow.com/questions/34845485/destroy-a-gameobject-when-it-enters-a-trigger-collider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!