I’m using a coroutine to manage NPC spawns, and it seems to my eyeballs that it runs and yields correctly, but every time I run the game it freezes up after a few seconds, and disabling this coroutine results in it no longer freezing. Do I have an obvious mistake in my implementation?
void Awake(){
StartCoroutine (managePedestrianPopulation());
}
IEnumerator managePedestrianPopulation(){
while (true) {
SpawnPedestrian(); //Check actual population density vs. desired population density, spawn new ones if under, remove old ones if way over
yield return new WaitForSeconds(2f);
}
}
void SpawnPedestrian(){
if (activeSpawners.Count > 0){ //If there are any active spawners in the game
int randomIndex = Random.Range (0, activeSpawners.Count); //Select a random spawner from the list of active ones
Transform newPedestrian = PoolManager.Pools ["Pedestrians"].Spawn ( //Pooling system: grab a pedestrian prefab from my pool and spawn it at the spawner selected above
pedestrianPrefab,
activeSpawners[randomIndex].t.position,
Quaternion.identity
);
}
}