- Home /
Destroy object on Trigger Enter
Hi all,
I have an object that I want to destroy when a ball enters its collider. The object is marked as trigger and has a tag named destroy. The script is attached to the object I want to be destroyed. The script is not working...what am I missing? Thanx and Cheers
function OnTriggerEnter (myTrigger : Collider) { if(myTrigger.gameObject.tag == "destroy") { Destroy(gameObject); } }
Answer by vexe · Sep 14, 2013 at 03:09 AM
You said you're attaching this script to the object you want to destroy, which has a tag of "destroy" - and yet in the OnTriggerEnter
you're checking for "destroy"? - Either do this, and attach it to the object you want to destroy (make sure you have isTrigger
checked on the collider of your object in order for OnTriggerEnter
to fire):
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == "ball")
Destroy(gameObject);
}
Or attach this to the ball instead:
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == "destroy")
Destroy(other.gameObject);
}
You're welcome! - If my answer solved your problem, please mark it as correct to close the question so that others don't think it's active and we can move on :)
Great! - Don't forget to mark the answer as correct. (The little 'correct' mark below the thumbs up/down icons)
Your answer
Follow this Question
Related Questions
Flinging Wall Death 1 Answer
Help using trigger - My item won't be destroyed and I keep going through it 1 Answer
Destroy a specific object on trigger? 1 Answer
HELP!!! I'm newbie. D: 1 Answer
Collider to Delete Object 1 Answer