Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by rendered_monkey · Mar 09, 2017 at 09:24 AM · coroutinewaitforsecondscoroutines

WaitForSeconds not working in IEnumerator function using MoveNext

I want to start N number of coroutines and wait for them all to resolve. The following seems to work with the exception of WaitForSeconds.

I have the following code with two DoSomething functions A and B I expect both to cause a wait of 5 seconds but only DoSomethingA waits the 5 seconds. DoSomethingB returns right away.

 IEnumerator DoSomethingA()
 {
   float time = Time.time;
   while (Time.time <= time + 5)
   {
     yield return null;
   }
 }
 
 IEnumerator DoSomethingB()
 {
   yield return new WaitForSeconds(5);
 }

 IEnumerator Start () {
   List<IEnumerator> waiting = new List<IEnumerator>();
 
   waiting.Add(DoSomethingA());
   waiting.Add(DoSomethingA());
   waiting.Add(DoSomethingA());
   waiting.Add(DoSomethingB());
   waiting.Add(DoSomethingB());
   waiting.Add(DoSomethingB());
 
   while(waiting.Count>0){
       for (int i = 0; i < waiting.Count; i++)
       {
         IEnumerator e = waiting[i];
       if (!e.MoveNext()) {
         waiting.Remove(e);
         i--;
         Debug.LogWarning("Done "+Time.time);
       }
       }
       yield return null;
   }
 }

Any idea why this is?

Comment
Add comment · Show 1
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 rendered_monkey · Mar 06, 2017 at 08:53 PM 0
Share

I think the issue is WaitForSeconds returns a YieldInstruction how would I Progress the YieldInstruction since it doesn't have a $$anonymous$$oveNext()?

1 Reply

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

Answer by Bunny83 · Mar 09, 2017 at 01:38 PM

All your DoSomething methods are not coroutines since you don't pass them to Unity's coroutine scheduler. They are just pure "generator methods". The point of a generator is usually to produce "things" which you can access through the Current property of the IEnumerator object after you called MoveNext.

Unity just uses generators and basically flip the logic around. Usually the things that you yield are what you're actually interested in. Between the yields there can be additional code which might be required for the generation of the content that should be returned.

A coroutine basically does the opposite. The main focus of a coroutine is the "code in between" yields. The yields are just cooperative yielding points where the control is yielded back to the caller. The actual "content" that is produced (the things that you yield) are simply used by the scheduler to determine when they have to continue this coroutine.

By iterating the IEnumerators yourself you basically take the spot of the coroutine scheduler. If that's not what you wanted you may want to actually start the coroutines with StartCoroutine, Since you want to wait for all coroutines this usually works:

 List<Coroutine> waiting = new List<Coroutine>();
 
 waiting.Add( StartCoroutine( DoSomethingA() ) );
 waiting.Add( StartCoroutine( DoSomethingA() ) );
 waiting.Add( StartCoroutine( DoSomethingA() ) );
 waiting.Add( StartCoroutine( DoSomethingB() ) );
 waiting.Add( StartCoroutine( DoSomethingB() ) );
 waiting.Add( StartCoroutine( DoSomethingB() ) );
 
 for (int i = 0; i < waiting.Count; i++)
     yield return waiting[i];
 
 Debug.Log("all done");

Of course you don't get feedback when the individual coroutines finish. If the first one takes the longest time to finish, all others will have finished before the first one. So that means once the longest has finished the for loop will "rush" through the others as they are finished already.

Anyways the debug log at the end is executed once all coroutines have completed.

Though if you want to create / use your own coroutine scheduler you can create one or use this alternative. Note that the "CoroutineScheduler" on the wiki is not "compatible" with Unity's coroutines as most of Unity's special yield values have the important information buried in private / internal or native fields.

The autor of that neat little CoroutineScheduler wrote an excelent blog article where he explains how an IEnumerator and Unity's coroutines work, Unfortunately the blog doesn't exist anymore. However we have the "wayback machine", so here's the cached article.

If you don't want to create your own coroutine scheduler / system but you want to get feedback after every coroutine, you might want to try my CoroutineHelper. It can "wrap" any coroutine in another coroutine and allows to give feedback when it's instance has finished. It runs all coroutine on a seperate singleton class to ensure they aren't destroyed too early. You can even "register" a callback that is called when the coroutine is done. Though it might be a bit overkill for what you want. Alternatively you can create your own "wrapper" like this:

 public class CoroutineEx
 {
     private IEnumerator m_Enumerator;
     private bool m_IsDone = false;
     public bool IsDone { get { return m_IsDone;}}
     public CoroutineEx(IEnumerator aCoroutine)
     {
         m_Enumerator = aCoroutine;
     }
     public IEnumerator Run()
     {
         while (m_Enumerator.MoveNext())
             yield return m_Enumerator.Current;
         m_IsDone = true;
     }
 }

With that class you can do it like this:

    List<CoroutineEx> waiting = new List<CoroutineEx>();
  
    waiting.Add(new CoroutineEx( DoSomethingA() ));
    waiting.Add(new CoroutineEx( DoSomethingA() ));
    waiting.Add(new CoroutineEx( DoSomethingA() ));
    waiting.Add(new CoroutineEx( DoSomethingB() ));
    waiting.Add(new CoroutineEx( DoSomethingB() ));
    waiting.Add(new CoroutineEx( DoSomethingB() ));
    
    for(int i = 0; i < waiting.Count; i++)
        StartCoroutine(waiting[i].Run()); // start each coroutine with the wrapper.
    
    while(waiting.Count > 0)
    {
        for (int i = waiting.Count-1; i >= 0; i--)
        {
            if (waiting.)
            {
                waiting.RemoveAt(i);
                Debug.LogWarning("Done "+Time.time);
            }
        }
        yield return null;
    }
    Debug.Log("All done");

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Coroutines WaitForSeconds – uneven spacing 2 Answers

Using coroutines to do attack patterns 1 Answer

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

yield return new WaitForSeconds () not working 1 Answer

Return value from coroutine to non monobehaviour 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