- Home /
How to Destroy game object after a few seconds then instantiate an explosion upon death?
Hello,
I`m new to Unity and programming and i have a problem I wrote a small crappy function where my game object dies after 3 seconds and instantiate an explosion. But it instantiates the explosion when I instantiate the game object that is supposed to die after 3 seconds.
What i want is to instantiate a torpedo, destroy it after a few seconds and instantiate an explosion.
I put the instantiation in start because if i put it in Update it instantiate a crap top of explosions for some reason when my torpedo is instantiated.
Does anyone know how to approach this ? Thank you ^-^
Here is my code :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RocketPlayer : MonoBehaviour {
public Rigidbody rocketRb;
public GameObject explode;
public float speed;
public bool isDeadRocket;
void Start ()
{
isDeadRocket = false;
rocketRb.velocity = transform.forward * speed;
if (isDeadRocket = true) {
Instantiate (explode, transform.position, transform.rotation);
}
}
void Update ()
{
}
public void RocketDeath ()
{
Destroy (gameObject, 3);
isDeadRocket = true;
}
}
Answer by KittenSnipes · Dec 16, 2017 at 12:54 AM
Wherever you use rocketdeath you can have a boolean after it that you can use in this class. So something like:
if (object == null) {
objectIsDestroyed == true;
}
In the class that uses RocketDeath and then use that boolean to instantiate the explosion at the death site.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers
Destroying Instantiated Prefab from AssetBundle 1 Answer
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer
Instantiate problem 1 Answer