Question by
xXSilverswordXx · Feb 08, 2017 at 10:56 PM ·
instantiateienumerator
Instantiate not repeating?
Here is the script:
public class Randomlinespawner : MonoBehaviour {
public GameObject BlueBolt;
void Start()
{
StartCoroutine(Linespawner());
}
private IEnumerator Linespawner()
{
Instantiate(BlueBolt, new Vector3(-20, 0, 0), Quaternion.Euler(0, 0, 0));
yield return new WaitForSeconds(0.3f);
}
}
The object 'bluebolt' has been set, however the instantiate only triggers once rather than once every 0.3 seconds. I am quite new to unity, and would like to know why this is not working.
Thanks! :)
Comment
Best Answer
Answer by Chikari · Feb 08, 2017 at 11:03 PM
You are starting a Coroutine, which spawns an object, waits, and then returns (="disappears"). If you want to repeat its function infinitely, you do it like this:
private IEnumerator Linespawner() {
while (true) {
Instantiate(BlueBolt, new Vector3(-20, 0, 0), Quaternion.Euler(0, 0, 0));
yield return new WaitForSeconds(0.3 f);
}
}