- Home /
Destroy object if get touched
I have a Character that moves forward. Now were I was looking for is: I have Obj_Char and Obj_Object
If Obj_Char touch Obj_Object, it destroys Obj_Object.
But I don't know what to do because I tried a lot of ways and I drive Crazy. Someone who can help me PLEASE? :D
Answer by Bluk · May 04, 2013 at 01:57 PM
Take a look at the colliders and OnCollisionEnter: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html?from=Collision
This is what I got right now, I attached this script to Obj_Object but it won't destroy if it get touched by Obj_Char
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag=="Obj_Char") {
Destroy(gameObject);
}
}
Can you help me please
If the object is marked as a trigger, will OnCollision still work? Something to test.
Well I really use other names. I tagged Obj_Char as ObjTag
And this is the Js I use;
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.tag=="ObjTag") {
Destroy(gameObject);
}
}
But it still doesn't work :S
Just tested, OnCollision events don't work with objects that are triggers. Is your object a trigger? Try:
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag=="ObjTag") {
Destroy(gameObject);
}
}
Bobble.