I’ve tried Coroutine (breaks somehow and never stops spawning. And I’ve tried InvokeRepeating but cant pass the transform param. for the location. Any suggestions be great.
void SpawnMinions()
{
foreach(Transform t in spawnPoints)
{
SpawnMinion(t);
}
print(enemyCount);
spawnTimer = Time.time + spawnDelay;
}
private void Update()
{
if(Time.time >= spawnTimer)
{
SpawnMinions();
}
}
void SpawnMinion(Transform spawnPoint)
{
for(int i = 0; i < numberToSpawn; i++)
{
Instantiate(minion, spawnPoint.position, Quaternion.Euler(0, 0, 0));
// I need a delay here, before the next spawn.(will give the minion time to move before next spawns)
}
}
void Start()
{
for( int i = 0; i < spawnPoints.Length; i++ )
{
StartCoroutine( SpawnMinion( spawnPoints[i] ) ); // Starts 1 coroutine for each spawn point
}
}
IEnumerator SpawnMinion( Transform spawnPoint )
{
for( int i = 0; i < numberToSpawn; i++)
{
Instantiate(minion, spawnPoint.position, Quaternion.Euler(0, 0, 0));
yield return new WaitForSeconds(spawnDelay); // yields this coroutine for spawnDelay game time
}
}
Doesn’t work, like I said Coroutine breaks it so they just keep spawning over and over. I cant call the coroutine in start, has to be in a if statement that checks the timer in the update. your example just spawns one wave and that’s it. That’s not a fun moba
public class BaseController : MonoBehaviour
{
GameObject minion;
float waveTimer = 0f; // timer that tells base when to spawn enemies.
float spawnDelay = .5f; // delay between each enemy spawn.
float waveDelay = 60f; // delay between each wave.
int numberToSpawn = 10;
int enemyCount = 0;
List<Transform> spawnPoints = new List<Transform>();
private void Awake()
{
spawnPoints.Add(transform.Find("BaseSpawn1"));
spawnPoints.Add(transform.Find("BaseSpawn2"));
spawnPoints.Add(transform.Find("BaseSpawn3"));
minion = Resources.Load("Prefabs/Enemy/Minion") as GameObject;
}
private void Update()
{
if(Time.time >= waveTimer)
{
for (int i = 0; i < spawnPoints.Count; i++)
{
StartCoroutine(SpawnMinion(spawnPoints[i])); // Starts 1 coroutine for each spawn point
}
waveTimer = Time.time + waveDelay;
}
}
IEnumerator SpawnMinion(Transform spawnPoint)
{
for (int i = 0; i < numberToSpawn; i++)
{
Instantiate(minion, spawnPoint.position, Quaternion.Euler(0, 0, 0));
yield return new WaitForSeconds(spawnDelay); // yields this coroutine for spawnDelay game time
}
}
}
Used your example, My’n would of worked, the first time I used a coroutine, but I forgot to initialize the WaveDelay, so it stayed at zero and kept the coroutine going constantly.