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?