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 27, 2020 at 09:01 PM by withlovegiftgiver for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by withlovegiftgiver · Oct 22, 2020 at 11:40 PM · scripting problemcoroutineloopwaitforseconds

Looping a Script

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.

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Oct 24, 2020 at 01:10 AM

@Klarzahs is spot on. This is the main issue in your code. However you should avoid using such tangled coroutines anyways. Starting coroutines is expensive and produces garbage. It's better to have a single coroutine and keep it running. Since you want an infinite cycle anyways your code can be simplified heavily

 void Start()
 {
     StartCoroutine(Rain());
 }
 
 IEnumerator Rain()
 {
     var wait = new WaitForSeconds(5f);
     while (true)
     {
         int timeUntilNextRainEvent = Random.Range(minimumTimeUntilNextRainEvent, maximumTimeUntilNextRainEvent);
         while (timeUntilNextRainEvent > 0)
         {
             timeUntilNextRainEvent --;
             yield return wait;
         }
         isRaining = true;
         int RainDuration = Random.Range(minimumRainDuration, maximumRainDuration);
         while (RainDuration > 0)
         {
             RainDuration--;
             yield return wait;
         }
         isRaining = false;
         yield return null;
     }
 }

Note the final yield return null; is a safety measure. In case your min and max values are smaller or equal to 0 none of your while loops would be entered. In this case you would never hit a yield statement and Unity would hang / freeze. Always makes sure you have a yield statement in your while loop.

Comment
Add comment · Show 1 · 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 Bunny83 · Oct 24, 2020 at 01:20 AM 0
Share

Of course if you want / need you can make the two counter variable member variables as you have it in your original script. Though if you don't really need them it's usually better to keep them encapsulated. It reduces unwanted errors.

avatar image
1

Answer by Klarzahs · Oct 23, 2020 at 11:59 PM

Hi @withlovegiftgiver ,

You redefine RainDuration in line 34 (StartRain()): int RainDuration = ...;. You thereby create a local variable, which is only valid within the StartRain() function scope.
I assume you set the global RainDuration in the inspector, it runs once and is then <0. You call StarRain() again, but as you redefine the variable, the global one does not get reset and is still <0. Ergo your rain immediately stops, as Duration() accesses the global variable.
Solution: Just drop the "int" ;)

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

254 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How does WaitForSeconds pass values? 0 Answers

Why isn't my simple coroutine working? (and how can I make it infinite?) 2 Answers

Problem with coroutine 2 Answers

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

Rerun a script after it finishes, when it already has multiple Coroutines? 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