I’m trying to destroy a explosion after three seconds but my method is not working. I make the explosion after I kill the enemy in the EnemyAI script. I find the script attached to the explosion and call the function in the script to destroy the game object.
EnemyAI Script
public class EnemyAI : MonoBehaviour {
public GameObject explosion;
void Death(){
Destroy (gameObject); //kill the enemy
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
Instantiate(explosion, transform.position, randomRotation);
explosion.gameObject.GetComponent<DestroyItems>().DestroyExplosion();
}
}
DestroyItems Script
public class DestroyItems : MonoBehaviour {
float lifetime = 3.0f;
public void DestroyExplosion ()
{ Destroy (gameObject, lifetime); }
}
There are no errors, but the explosion does not disappear. I get a warning saying to use DestroyImmediate(theObject, true) which I don’t want to use because it will cause problems later with missing assets in game folder.
Please avoid using IEnumerator and Coroutine in your answers. I’m just looking for something simple and I don’t need complex answers because I know there must be a easy way to do this. Thanks