why does the console out put say, 28,29,30 and not just 30?? … while I’m posting is there any better way to get the same result but with a different approach? I heard calling coroutine is bad ideal for the update method.
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
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
enemyCount++;
}
print(enemyCount);
}
}