Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

WaitUntil() or While() in coroutine

Discussion in 'Scripting' started by Ali_Akbar_Montazeri, Aug 14, 2019 at 9:03 AM.

  1. Ali_Akbar_Montazeri

    Ali_Akbar_Montazeri

    Joined:
    Dec 6, 2014
    Posts:
    179
    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,104
    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,022
    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:
    179
    I see, so they are just the same but the first one is a little bit heavy right? Thank you!