Hi all,
I want to instantiate some gameobjects (let’s say lightning, explosions, etc) that will appear and last for some given time.
So far, I can instantiate and destroy the gameobject on the Start function but when I try to do the same on Update (that’s what I need) the scene hangs and never destroy the objects so it creates prefabs forever.
Here is the code:
using UnityEngine;
using System.Collections;
public class IntermittentShit : MonoBehaviour {
public GameObject shitPrefab;
public GameObject shitPosition;
public float time; //the time to appear
public float time2; // the time to destroy the object
IEnumerator LightningMethod(){
yield return new WaitForSeconds(time);
foreach (Transform child in transform) {
GameObject enemy = Instantiate (shitPrefab, child.transform.position, child.transform.rotation) as GameObject;
enemy.transform.parent = child;
yield return new WaitForSeconds(time2);
Destroy (gameObject);
}
}
void Start () {
}
void Update() {
StartCoroutine(LightningMethod());
}
}
What am I doing wrong? Do you know any other method to create intermittent objects (light, lightning, smoke, etc) better than this?
Thank you very much guys.
Regards,