I was hoping someone could help me out with an issue I am having. I am trying to get my spawner coroutine (waitAndSpawn) to work whilst my boolean variable gamePlaying = true. My coroutine is in a script called ‘LevelManager’ and the gamePlaying variable is in a different script called ‘GameStatus’. The variable gamePlaying is initially set to false on Awake (where instructions are presented), then set to true on Start (of GameStatus script).
When calling StartCoroutine in the Start method (of LevelManager script), the coroutine does not work. However, when I call StartCoroutine in the Update method (of LevelManager script), my coroutine works but a million things spawn each second.
I have no idea why my coroutine isn’t working properly (i.e., when the player starts the game, things spawn after a specified delay, then spawn again) - any help is appreciated!
public class GameStatus: MonoBehaviour
{
public bool gamePlaying;
}
public class LevelManager : MonoBehaviour
{
public GameStatus status;
private void Start()
{
StartCoroutine(waitAndSpawn()); // coroutine does not work
}
private void Update()
{
// StartCoroutine(waitAndSpawn()); // coroutine works but spawns so many things per second
}
IEnumerator waitAndSpawn()
{
status = FindObjectOfType<GameStatus>();
while (status.gamePlaying == true)
{
SpawnStimuli();
yield return new WaitForSeconds(spawnDelay);
}
}
}