x


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

more ▼

asked Aug 20 '12 at 12:16 PM

Bootsgaloore gravatar image

Bootsgaloore
21 5 8 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Aug 20 '12 at 12:26 PM

Khada gravatar image

Khada
2.2k 2 9

i got an error can you be more specific please (BCE0005: Unknown identifier: 'enemyGameObject'.)

Aug 20 '12 at 12:40 PM Bootsgaloore

'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.

Aug 20 '12 at 12:43 PM Khada

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; } }

Aug 20 '12 at 12:45 PM Bootsgaloore

You got errors? What error? Tell us what error is popping up.

Aug 20 '12 at 01:14 PM tigerfoot

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."

Aug 20 '12 at 01:24 PM tigerfoot
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Aug 20 '12 at 01:30 PM

jordo834 gravatar image

jordo834
1

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

Aug 20 '12 at 01:34 PM tigerfoot
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3317
x1089
x764
x18
x9

asked: Aug 20 '12 at 12:16 PM

Seen: 282 times

Last Updated: Aug 20 '12 at 01:37 PM