- Home /
Destroy an enemy when it touches an object
hi im stuck and im wondering if anyone could help.
im absolutely useless with script .. i need and object to dissapear when it touches an object
Thanks
Answer by Khada · Aug 20, 2012 at 12:26 PM
Easy. Wherever it is that you detect the collision, put this:
GameObject.Destroy(enemyGameObject);
If you're using the OnCollision method, you can get the object via Collision.gameObject
EDIT:
Haven't checked the code but this should work.
function OnTriggerEnter (other : Collider)
{
if (other.CompareTag ("Player"))
{
other.transform.position = destination.transform.position;
}
else //must be enemy
{
GameObject.Destroy(other.gameObject);
}
}
Highly suggest you go learn the basics of programming. You'll have a very hard time making a game without some fundamental knowledge.
i got an error can you be more specific please (BCE0005: $$anonymous$$ identifier: 'enemyGameObject'.)
'enemyGameObject' needs to be replaced with whichever game object is your enemy. How you do this depends on the context. I would need much more detail to tell you exactly how to do it for your situation.
Im planning on knocking this object off to a teleporter .. but i also use this script to teleport the player ... heres the script
JS
var destination : Transform;
function OnTriggerEnter (other : Collider) { if (other.CompareTag ("Player")) { other.transform.position = destination.transform.position; } }
You got errors? What error? Tell us what error is popping up.
If it didnt print anything the collision never occurs. Do you have a rigidbody attached to one of the colliding objects?
Quote from Unity Docs: "... trigger events are only sent if one of the colliders also has a rigidbody attached."
Answer by jordo834 · Aug 20, 2012 at 01:30 PM
What script are you using to detect the collision? You should use the Collision or RayCastHit or whatever youre using, normally called "hit" to get the enemy gameobject, so try replacing enemyGameObject with hit.gameObject
It doesnt matter. That function "OnTriggerEnter" is never called because the collision is not registered. The problem is somewhere else. Perhaps he didnt attach rigidbody component to neither object