- Home /
Question is off-topic or not relevant
Rerun a script after it finishes, when it already has multiple Coroutines?
I've seen a lot of people trying to loop scripts before, but a lot of them won't work in my case because my script uses a lot of voids and Coroutines as is. I'm wondering if there is a way to rerun the script after is finishes infinitely, while changing the script as little as possible. It would be best if I could add one or two lines of script to make it repeat. I've tried to add a line of code at the end of the last function to set a value that would restart the script, but after the first section was run the second time nothing else would work properly after that.
Here is my code:
public void StartRainEventCountdown()
{
timeUntilNextRainEvent = Random.Range(minimumTimeUntilNextRainEvent, maximumTimeUntilNextRainEvent);
countdownStarted = true;
StartCoroutine(Countdown());
}
private void StartRain()
{
isRaining = true;
int RainDuration = Random.Range(minimumRainDuration, maximumRainDuration);
StartCoroutine(Duration());
}
private void StopRain()
{
Debug.Log("Stopped rain");
isRaining = false;
}
private IEnumerator Countdown()
{
Debug.Log("ya got 5 seconds before PAIN");
while (timeUntilNextRainEvent > 0)
{
timeUntilNextRainEvent -= 1;
yield return new WaitForSeconds(5);
}
StartRain();
}
private IEnumerator Duration()
{
Debug.Log("ya got 5 seconds before ya good");
while (RainDuration > 0)
{
RainDuration -= 1;
yield return new WaitForSeconds(5);
}
StopRain();
}
Answer by Aviryx · Aug 14, 2020 at 10:02 PM
This should reduce the time until next rain event by 1 minute (every 5 seconds) until you reach 0 and then set a new "time until next rain event". See if that works in regards to getting a coroutine to do something every x seconds/minutes. Writing this all off the top of my head but I don't think I've made any mistakes.
public class RainManager : MonoBehaviour
{
public bool isRaining;
// how many minutes until the next rain event
public int timeUntilNextRainEvent;
// the minimum and maximum amount of time between events
public int minTimeBetweenRainEvents;
public int maxTimeBetweenRainEvents;
public bool waitingForCountdown;
void Update()
{
if (timeUntilNextRainEvent == 0)
{
waitingForCountdown = true;
SetNewRainEventTime();
}
if (!waitingForCountdown)
{
if (timeUntilNextRainEvent > 0)
{
StartCoroutine(CountdownToNextRainEvent());
}
}
}
private void SetNewRainEventTime()
{
timeUntilNextRainEvent = Random.Range(minTimeBetweenRainEvents, maxTimeBetweenRainEvents);
Debug.Log("Time until next rain event:" + timeUntilNextRainEvent + " minutes");
StartCoroutine(CountdownToNextRainEvent());
}
private IEnumerator CountdownToNextRainEvent()
{
waitingForCountdown = true;
yield return new WaitForSeconds(5);
timeUntilNextRainEvent -= 1;
Debug.Log(minutesUntilNextRainEvent + " minutes left until next event");
waitingForCountdown = false;
}
}
What you described in the first instance is actually what I tried already, and what happened was it looped, but as soon as it got past the IEnumerator Countdown it just kind of spammed everything else in the script. I get a lot of messages in the console telling me things like "stopped rain", "start rain" whatever else I write to console, but all at the same time.
I also tried the IEnumerator someCoroutine() but I have no doubt that I don't know what I'm doing on that one. What is it exactly trying to do?
There are certain cases where a coroutine will just continue running when invoked so sometimes you need to enforce a check that will stop it running again based on a specific condition. I updated my answer with an example. I think the Duration() coroutine may have been invoking multiple StopRain() functions.
As far as I can tell, the issue is actually not that it won't loop, nor that the Coroutines are simply spam$$anonymous$$g. It seems like the script as is is working as intended, but for some reason the wait times are not happening after the first time. Any idea why this might be?
Not sure what you mean "after the first time". Do you mean literally after the first "wait 5 seconds" it does not wait again, or do you mean that it successfully cycles through 1 rain event and then the wait times are not working?
It successfully cycles once and then no longer waits.