- Home /
Coroutine for zombie spawner not working, any help apreciated
Hi I am quite new to this so I need a bit of help I want spawner that delay's the spawn for a set amout of time. So I decided to use a coroutine to delay the spawn. but for some reason that i can figure out its not executing. here is the code for my zombie spawner script.
using UnityEngine; using System.Collections;
public class ZombieSpawner : MonoBehaviour {
public int EnemysPerMinute; private float TimeBetweenSpawn; public GameObject Zombie;
// Use this for initialization void Start () {
TimeBetweenSpawn = 60 / EnemysPerMinute;
StartCoroutine(Spawn_Zombie());
}
IEnumerator Spawn_Zombie() {
yield return new WaitForSeconds(TimeBetweenSpawn);
Instantiate(Zombie, transform.position, Quaternion.identity);
}
}
Answer by David C · Apr 30, 2011 at 04:22 PM
hey I managed to get it to work, I dont really know how it just sort of started working. Anyways can I ask how can I get it to repeat itself again and again? thanks all
Answer by David C · May 01, 2011 at 07:50 AM
Yes Sorry i Should post the new code, i couldnt get invoke repeating to work so i went for a different approach
in "void start()"
i have StartCoroutine(Spawn_Zombie());
and then i have the actual coroutine
// spawns the zombie after certain time an then repeats IEnumerator Spawn_Zombie() {
yield return new WaitForSeconds(TimeBetweenSpawn);
Instantiate(Zombie_Left, transform.position, Quaternion.identity);
StartCoroutine(Spawn_Zombie());
} //terminates the zombie spawner after certain time IEnumerator Stop_Spawning() {
yield return new WaitForSeconds(TimeToSpawnEnemys);
Destroy(this.gameObject);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613130513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Zombie Wave Spawning 1 Answer
Spawn help? 0 Answers
Zombie Spawner script 2 Answers
Unity3D and C# - Coroutines vs threading 4 Answers
Synchronization in execution of methods of a class using co-routines. 1 Answer