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 /
avatar image
0
Question by Woj_Gabel_FertileSky · Dec 18, 2011 at 07:21 PM · coroutineloopmultipleyieldinterval

Running multiple coroutines in loops with different time

Hi! Im having some problems with understanding coroutines. My goal is to have multiple coroutines in while(true) loop with those couroutines running on different(and dynamic) intervals. Right now i have:

  function Start()
     {
        Loop();
     }
 
 function Loop()
 {
    while(true)
    {
      Do1;
      Do2;
      Do3;
      Do4;
     //...
    }
 }
 function Do1()
 {
    WaitForSeconds(4);
    Print("This will print every 4 seconds");
 }
 function Do2()
 {
    WaitForSeconds(2);
    Print("This will print every 2 seconds");
 }
 function Do3()
 {
    WaitForSeconds(14.34);
    Print("This will print every 14.34 seconds");
 }
 ...

 

Of course that there is an infinite loop, without any "yield" in it. Or maybe not? Placement of yield is bit difficult to understand for me. Anyone know how to achieve this?

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

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by aldonaletto · Dec 18, 2011 at 07:52 PM

The coroutine master here is @Eric5h5, but as far as I know, you should do it this way:

function Start(){ Do1(); // fire coroutine 1 Do2(); // fire coroutine 2 Do3(); // fire coroutine 3 }

function Do1(){ while (true){ Print("This will print every 4 seconds"); yield WaitForSeconds(4); // return control to the system, and resume here in 4 seconds } }

function Do2(){ while (true){ Print("This will print every 2 seconds"); yield WaitForSeconds(2); } }

function Do3(){ while (true){ Print("This will print every 14.34 seconds"); yield WaitForSeconds(14.34); } } From my experience, coroutines work this way: when you call a coroutine (a routine that contains at least one yield), the system creates a new coroutine instance and executes it until the first yield is found; at this point, the coroutine is interrupted and control returns to the caller routine; the instance created will be resumed automatically each new update cycle in the instruction following the yield, and execute until a yield is found, when it stops executing and let the system run free until the next update cycle occurs.
In the case above, Start will call coroutine Do1, which will return to Start when the first yield is found; Start will then call coroutine Do2, which will also return in the first yield, and finally it will call coroutine Do3, which will return in the first yield like the others. Since there are no more instructions, Start ends - but the coroutines started are still running, and continue printing messages at the defined intervals due to the while (true) infinite loop.

Comment
Add comment · Show 2 · 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 gregzo · Dec 18, 2011 at 08:02 PM 0
Share

@aldonaletto : I am much less experienced than you, but from the testing I've done for my music project in Unity, InvokeRepeating is much more precise than WaitForSeconds. Is there a downside I don't know of?

avatar image aldonaletto · Dec 19, 2011 at 02:00 PM 0
Share

No, I just told the OP how to do it with coroutines because that's what he asked. I believe you're right about InvokeRepeating: it may be way more precise than WaitForSeconds. Coroutines run in the update cycle, which occurs at the frame rate - not a stable reference, for sure! I don't know how they implement InvokeRepeating (not with coroutines, according to @Eric5h5), but from the results you've presented in a question of yours, it's very precise.

avatar image
2

Answer by gregzo · Dec 18, 2011 at 07:45 PM

Maybe you need to use InvokeRepeating("yourFunction",delayInSeconds,frequencyInSeconds); it's very precise. It basicaly calls your function at fixed time intervals after a delay. Precision is not perfect, but pretty good for most uses. Use CancelInvoke() to stop calling. It's in the docs too. P.S.:InvokeRepeating doesn't need your function to have a yiel statement. In your script, you could add yield WaitForSeconds(timeInSeconds); but it is less precise.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

While loop yield lag? 1 Answer

Pause the coroutine inside a loop 1 Answer

Coroutine Loops 1 Answer

Instantiating many objects during update lags 1 Answer

Yield WaitForSeconds doesn't work, give syntax error 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