Rocket to disappear after a few seconds?


I Have two problem that is driving me nuts.

  1. I need my rockets to disappear after a few seconds and starting an explosion animation, i have tried using the code destroy gameObject but then it destroys the whole ship i need a code that destroys my rockets (which are prefabs)

  2. I also need a delay from when i can fire again.

var prefabRocket:Transform;

var shootForce: float;

function Update () {

         var instanceRocket = Instantiate(prefabRocket, GameObject.Find("fighter/spawn1").transform.position, transform.rotation );
         instanceRocket.rigidbody.AddForce(transform.forward * shootForce);



Your help is much appreciated....

Easy way to destroy after a few seconds is to do this when you instantiate:

Destroy(instanceRocket, 3.0);

where 3.0 in my example is 3 seconds for it to live

on top of that, a simple way to start a particle explosion when it destroys is to just add a script with an OnDisable function, and instantiate a particle system in that

call it TimedObjectDestructor.js put the code in a rocket prefab the set it to like 5 or so what ever you want and were its says detach children is if its ticket it will make the rocket last forever so don't tick it unless you need to.

var timeOut = 1.0;
var detachChildren = false;

function Awake ()
    Invoke ("DestroyNow", timeOut);

function DestroyNow ()
    if (detachChildren) {
        transform.DetachChildren ();
    DestroyObject (gameObject);