Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 22, 2020 at 11:33 PM by withlovegiftgiver for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by withlovegiftgiver · Aug 14, 2020 at 09:36 PM · scripting problemcoroutineloopevent

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();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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;

     }

 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image withlovegiftgiver · Aug 14, 2020 at 10:14 PM 0
Share

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.

avatar image withlovegiftgiver withlovegiftgiver · Aug 14, 2020 at 10:19 PM 0
Share

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?

avatar image Aviryx withlovegiftgiver · Aug 14, 2020 at 10:31 PM 0
Share

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.

Show more comments
avatar image withlovegiftgiver · Aug 15, 2020 at 08:17 PM 0
Share

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?

avatar image Aviryx withlovegiftgiver · Aug 16, 2020 at 03:28 PM 0
Share

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?

avatar image withlovegiftgiver Aviryx · Aug 17, 2020 at 02:56 AM 0
Share

It successfully cycles once and then no longer waits.

Follow this Question

Answers Answers and Comments

245 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change a large number of booleans as fast as possible without sacrificing framerate? 1 Answer

Looping a Script 2 Answers

Simplify this code 3 Answers

Gamecontroller "Awake" instantiates other objects - Event order question 2 Answers

make events occur only at specific times 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges