Hi, I’m having an issue with my code where I’m trying to spawn enemies between a certain time frame the way I’m doing this is when that time period is true set a bool to true triggering a StartCoroutine. The issue is with this solution they will infinitely spawn while the bool is true. If I disable the bool right evert the enemies are spawning or use a WaitForSeconds after spawning, The enemies are not spawned at all both scripts work independently and out separate scripts. Both are below with the timer on top and the spawning code below it. Thank you so much for your advice and help and I appreciate it so much. If you have any questions or possible solutions I would love to hear them. Thank you all very very much.
[SerializeField] private Gradient lightColor;
[SerializeField] private GameObject light;
private int days;
public int Days => days;
private float time = 50;
private bool canChangeDay;
[HideInInspector]
public bool isNight;
private bool spawning = false;
public delegate void OnDayChanged();
public OnDayChanged DayChanged;
public static DayNightCycle instance;
void Start()
{
instance = this;
spawning = false;
isNight = false;
HUD.instance.TimeText.text = "Day";
}
void Update()
{
Debug.Log(time);
if (time > 500)
{
time = 0;
}
if(spawning = false && (int)time > 200 && (int)time < 300)
{
isNight = true;
Debug.Log("night");
spawning = true;
HUD.instance.TimeText.text = "Night";
}
else
{
isNight = false;
spawning = false;
// Debug.Log("day");
}
if((int)time == 250 && canChangeDay)
{
canChangeDay = false;
DayChanged();
days++;
}
if((int)time == 255)
canChangeDay = true;
time += Time.deltaTime;
light.GetComponent<Light2D>().color = lightColor.Evaluate(time * 0.002f);
}
IEnumerator Night()
{
Debug.Log("night");
yield return new WaitForSeconds(1f);
spawning = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaveSpawner : MonoBehaviour
{
[System.Serializable]
public class Wave
{
public Enemy[] enemies;
public int count;
public float timeBetweenSpawns;
}
public Wave[] waves;
public Transform[] spawnPoints;
public float timeBetweenWaves;
private bool StartNight;
private Wave currentWave;
private int currentWaveIndex;
private Transform player;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
// StartCoroutine(StartNextWave(currentWaveIndex));
}
void Update()
{
if(DayNightCycle.instance.isNight == true)
{
StartCoroutine(StartNextWave(currentWaveIndex));
Debug.Log("night");
}
else
{
return;
}
}
IEnumerator StartNextWave(int index)
{
yield return new WaitForSeconds(timeBetweenWaves);
StartCoroutine(SpawnWave(index));
}
IEnumerator SpawnWave(int index)
{
currentWave = waves[index];
for (int i = 0; i < currentWave.count; i++)
{
if (player == null){
yield break;
}
Enemy randomEnemy = currentWave.enemies[Random.Range(0, currentWave.enemies.Length)];
Transform randomSpawn = spawnPoints[Random.Range(0, spawnPoints.Length)];
Instantiate(randomEnemy, randomSpawn.position, randomSpawn.rotation);
yield return new WaitForSeconds(currentWave.timeBetweenSpawns);
}
}
}