I have the following script all set up and working, until it gets to the end and it tries to loop:
public bool isRaining;
public int minimumTimeUntilNextRainEvent;
public int maximumTimeUntilNextRainEvent;
public int timeUntilNextRainEvent;
public int minimumRainDuration;
public int maximumRainDuration;
public int RainDuration;
private bool countdownStarted;
//Sets the time until the next rainfall
public void StartRainEventCountdown()
{
Debug.Log("Set time");
timeUntilNextRainEvent = Random.Range(minimumTimeUntilNextRainEvent, maximumTimeUntilNextRainEvent);
countdownStarted = true;
StartCoroutine(Countdown());
}
//Starts the countdown until the next rainfall
private IEnumerator Countdown()
{
Debug.Log("Countdown to rain");
while (timeUntilNextRainEvent > 0)
{
timeUntilNextRainEvent -= 1;
yield return new WaitForSeconds(5);
}
StartRain();
}
//Starts rainfall and choses duration
private void StartRain()
{
Debug.Log("Started rain");
isRaining = true;
int RainDuration = Random.Range(minimumRainDuration, maximumRainDuration);
StartCoroutine(Duration());
}
//Countdown until clear skies
private IEnumerator Duration()
{
Debug.Log("counting til clear sky");
while (RainDuration > 0)
{
RainDuration -= 1;
yield return new WaitForSeconds(5);
}
StopRain();
}
//stops rainfall and resets the script
private void StopRain()
{
Debug.Log("Stopped rain");
isRaining = false;
StartRainEventCountdown();
}
What exactly happens is it successfully runs through it once, but on the second run it only runs IEnumerator Countdown successfully before it runs the rest of the script on loop with no wait times. Basically it waits every time its told at first, waits through the first coroutine again, and then ceases to wait.