- Home /
IEumerator not recognising when a bool has switched
I'm trying to make a system where enemies will only spawn at night time. My spawner has a bool passed into by the Day/Night cycle script but my CoRoutine isn't recognising when that bool changes from false to true. This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner : MonoBehaviour {
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnMinWait;
public bool stop;
Sun sun;
int randEnemy;
public int startWait;
// Use this for initialization
void Start () {
sun = GameObject.Find("SunLightCycle").GetComponent<Sun>();
StartCoroutine(waitSpawner());
}
// Update is called once per frame
void Update () {
spawnWait = Random.Range(spawnMinWait, spawnMostWait);
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds(startWait);
while (sun.isNight())
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[0], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds(spawnWait);
}
}
}
If I change my scene so it starts at night time they will spawn but when it changes from day to night they will not spawn, any ideas?
@Lewey100 Please could you include the logic used to calculate sun.isNight()
in your Sun.cs script? We can't gather any information from the code you've added here. Also is yield return new WaitForSeconds(startWait);
supposed to be yield return new WaitForSeconds(spawnWait);
?
I've edited my code to include the full script so far. The isNight() call works fine, I tested it in Update() and it will return true when it turns night. I'm just having difficulty letting my coroutine know it has changed!
You need to call itself, otherwise IEnumerator will end.
IEnumerator waitSpawner()
{
yield return new WaitForSeconds(startWait);
if(sun.isNight())
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[0], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
}
StartCoroutine(waitSpawner());
}
Answer by FortisVenaliter · Aug 07, 2017 at 04:06 PM
Your coroutine will only run while it is night, as the code in the while loop states. That's why it stops running afterwards.
You probably want the loop to run while the game is running, but only spawn if it's night.
I fixed it by dropping a bool in the coroutine and checking if that was true/false in update:
void Update () {
spawnWait = Random.Range(spawn$$anonymous$$inWait, spawn$$anonymous$$ostWait);
if(sun.isNight() && !coroutinerunning)
{
StartCoroutine(waitSpawner());
}
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds(startWait);
coroutinerunning = true;
while (sun.isNight())
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[0], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds(spawnWait);
}
coroutinerunning = false;
}
Your answer
Follow this Question
Related Questions
How Return Or Restart A Coroutine When A Variable Increase? 0 Answers
I can't start a coroutine. I get a weird message from visual studio 1 Answer
How Return Or Restart A Coroutine When A Variable Increase? 1 Answer
Wait for seconds and then set a bool to true. 2 Answers
Waiting twice inside coroutine (C#) 2 Answers