- Home /
Rocket to disappear after a few seconds?
HI!
I Have two problem that is driving me nuts.
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)
I also need a delay from when i can fire again.
var prefabRocket:Transform;
var shootForce: float;
function Update () {
if(Input.GetButtonDown("Jump")) { var instanceRocket = Instantiate(prefabRocket, GameObject.Find("fighter/spawn1").transform.position, transform.rotation ); instanceRocket.rigidbody.AddForce(transform.forward * shootForce);
}
}
Your help is much appreciated....
Answer by Mike 3 · May 19, 2010 at 11:01 PM
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
I tried using Destroy(instanceRocket, 3.0); but i got this error cant remove transform because Rigidbody depends on it?
Answer by josif · May 20, 2010 at 12:47 AM
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); }
Your answer
![](https://koobas.hobune.stream/wayback/20220613011149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Want to shoot to make an explosion 2 Answers
make explosion work 2 Answers
Destroy objects when key is pressed 1 Answer
Missile Collision and Explosions 1 Answer
how to add time ? (how to show a guiTexture and hide it after some time?) 0 Answers