How do i get my object to explode and destroy?

So I have a sentry bot that slowly creeps up on you and I want it to explode when it collides with the player. I have a particle system I wanna use as the explosion, and wrote a script to make it explode and destroy the sentry bot. However its not working…

var explosion : ParticleSystem; 

function OnCollisionEnter(){
var expl = Instantiate(explosion, transform.position, Quaternion.identity);
Destroy(expl, 3);

any pointers?

The Script is sound, My guess is the collision is not registering. I’m guessing neither your player or your bot have rigidbodies. Try adding a Rigidbody to one. Additionally I’d suggest Turning the collider into a trigger. This kind of thing is what triggers are made for and will give you more flexibility. Just enabled Trigger on the bot’s collider and change OnCollisonEnter to OnTriggerEnter

Hi Rick. You’d need to give us a little more detail so we can figure out the problem - it’s not working is kinda vague. Looking at the code, I think the sentry bot will explode the moment it collides with anything, so you’d need to specify the player. The code (not tested) would go something like this:

function OnCollisionEnter(collision : Collision) 
   // Check the bot has collided with player
   if(collision.gameObject.tag == "Player")
       // Instantiate Explosion
       Instantiate(explosionPrefab, pos, rot);
       // Destroy the gameobject
       Destroy (gameObject);

Destroying the gameobject MAY interfere with the instantiated explosion, so you may need to work around this. Also, if you set your particle emitter to one shot, you won’t need to call destroy on it.