- Home /
Weird Explosion Effect
So I am trying to spawn a grenade and have it explode. I have the instantiate thing down, but when I throw the grenade the particle effect plays before the grenade destroys itself. The CODE:
var grenadeTime : float = 5;
var explosion : GameObject;
function LateUpdate()
{
Destroy(gameObject, grenadeTime);
Instantiate(explosion, transform.position, transform.rotation);
}
Answer by Arithan · Feb 16, 2013 at 04:43 AM
With the way you currently have it, the grenade doesn't get destroyed until after 5 seconds. So you're instantiating the explosion before the grenade is destroyed. If you want it to be destroyed in the same frame, just use Destroy(gameObject).
More information about Destroy can be found in the docs:
http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Right, after all it is a grenade and I want it to explode after 5 seconds. But how do I make it so that the explosion plays only once when it was destroyed?
One way would be to use a timer in Update() like:
fuseTime -= Time.deltaTime;
if (fuseTime <= 0)
{
Destroy(gameObject);
// code for explosion
}
But I'm sure there are better ways to do it that doesn't involve checking every frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092804im_/https://answers.unity.com/themes/thub/images/avi.jpg)