Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

yield return with not null

Discussion in 'Scripting' started by Lazy_Sloth, Sep 9, 2020.

  1. Lazy_Sloth

    Lazy_Sloth

    Joined:
    Apr 3, 2018
    Posts:
    39
    Hello,

    I'm just curious what if I'm using the yield return with a number.. for example 'yield return 10;'

    I tried to looking for it but people everywhere talking about yield return null only... but I didn't get error when I use it with a number instead null. Can you explain me this please?

    Basically I'm looking for a solution where I can wait more than the next frame.. 10-20 frames before call it again for optimization.. I know about WaitForSeconds(), but there should be a different solution which use frames, not seconds...

    Thanks!
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You can yield return pretty much anything you want, and Unity will resume your coroutine next frame, equivalent to
    yield return null;
    . The only exception to this is if you yield return one of the following:
    • A special yield instruction such as WaitForSeconds or WaitUntil, etc...
    • Another coroutine, in which case it will yield until that coroutine has finished
    You could use WaitUntil to wait a number of frames as mentioned above, but I think this is simpler:

    Code (CSharp):
    1. int numberOfFramesToWait = 20;
    2. while (numberOfFramesToWait > 0) {
    3.   numberOfFramesToWait -= 1;
    4.   yield return null;
    5. }
    Depending on your use case, check out WaitForSeconds too, as that may be a little more regular in terms of real time, if that matters for your application.
     
    Suddoha and Homicide like this.
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    To expand on @PraetorBlue's answer with regards to this part of your question:

    You did not get an error because everything is basically an object, and the non-generic IEnumerator interface expects a System.Object (or keyword: object) to be returned via its property "Current". So it's legit to yield return really anything you want.. null, an integer, a float, a vector, an object, a component, a gameobject... really anything.

    However, one thing is special when you return "Value Type" values... It requires boxing, which kind of wraps a "Value Type" value into a System.Object so that it can be treated as one, in other words, heap-memory needs to be allocated for that object. That's the simple and short version of it.

    So yeah, it's basically an allocation that is easy to avoid, and therefore you just "yield return null".
     
    Last edited: Sep 9, 2020
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,766
    As @PraetorBlue points out above, the only reason anything you return has meaning is because by agreement, StartCoroutine() uses rules as to when it gets back again to your coroutine.

    You can manually "pump" a coroutine yourself if you like by calling its .MoveNext() method and doing whatever you like with the value returned from it.

    For 10 or 20 frames, yield that many nulls. For an amount of time, yield the WaitForSeconds() equivalent.
     
  6. Lazy_Sloth

    Lazy_Sloth

    Joined:
    Apr 3, 2018
    Posts:
    39
    Thanks guys! very clear answers. I didn't know about the WaitUntil, this is what I need.. but PraetorBlue's solution also a very good one!