Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

WaitUntil() or While() in coroutine

Discussion in 'Scripting' started by Ali_Akbar_Montazeri, Aug 14, 2019.

  1. Ali_Akbar_Montazeri

    Ali_Akbar_Montazeri

    Joined:
    Dec 6, 2014
    Posts:
    187
    Hi I have a simple question. What's the different between
    WaitUntil(condition)
    and
    while(condition)
    ?
    In my real case I want to wait until
    Caching 
    is ready. So I'm just curious which one is better between those two? Thank you.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,755
    I assume you mean the difference between:

    Code (csharp):
    1. yield return WaitUntil(() => condition);
    and
    Code (csharp):
    1. while(!condition)
    2.     yield return null;
    ?

    Because in that case they're equivalent, except that the first one allocates memory for the lambda.

    .. Also it might be that the first one always waits at least one frame, I'm unsure at the moment.
     
    Ali_Akbar_Montazeri likes this.
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,734
    Code (CSharp):
    1. yield return new WaitUntil(condition);
    is just a shortcut for

    Code (CSharp):
    1. while(!condition) yield return null;
    It makes no sense what to chose in general.
     
  4. Ali_Akbar_Montazeri

    Ali_Akbar_Montazeri

    Joined:
    Dec 6, 2014
    Posts:
    187
    I see, so they are just the same but the first one is a little bit heavy right? Thank you!
     
unityunity