Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to wait for a frame in c#?

Discussion in 'Scripting' started by Viskag, Jun 9, 2009.

  1. Viskag

    Viskag

    Joined:
    Apr 20, 2009
    Posts:
    46
    in java:
    functionABC()
    {
    ...;
    yield;
    ...;
    }
    how to wait for "just a frame" in c#?
    thx
     
    elektrofobi likes this.
  2. DigitalGlass

    DigitalGlass

    Joined:
    May 12, 2009
    Posts:
    86
    That1phillps and lv2lrn4lf_unity like this.
  3. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    or another way:
    var i : int = 0;

    function update () {

    i=i+1;

    if (i==2) {
    print("this message prints every second frame");
    i=0;
    }

    }
     
    That1phillps and astracat111 like this.
  4. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    Update can run many times per frame, but it's close.

    You could try to set something up with FixedUpdate, which runs every frame, based on a set frame rate.
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,327
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,145
    No, just once per frame.

    Don't use FixedUpdate for anything except physics code; that's what it's for.

    Anyway, the correct answer to the original question is "yield return null". :) (You can make Start return an IEnumerator; you don't have to start a coroutine.)

    --Eric
     
    NeatWolf and Plystire like this.
  7. Viskag

    Viskag

    Joined:
    Apr 20, 2009
    Posts:
    46
  8. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    My bad, ignore late night advice.

    Update runs many times per second,(many frames per second), and I got turned around.
     
  9. drussilla

    drussilla

    Joined:
    Mar 20, 2014
    Posts:
    9
    Here is small class to wait for some amount of frames:
    Code (CSharp):
    1. public static class WaitFor
    2. {
    3.     public static IEnumerator Frames(int frameCount)
    4.     {
    5.         while (frameCount > 0)
    6.         {
    7.             frameCount--;
    8.             yield return null;
    9.         }
    10.     }
    11. }
    Here is how you can use it:
    Code (CSharp):
    1. public IEnumerator CoroutineAction()
    2. {
    3.     // do some actions here  
    4.     yield return StartCoroutine(WaitFor.Frames(5)); // wait for 5 frames
    5.     // do some actions after 5 frames
    6. }
    Found this class here: http://druss.co/2015/05/unity3d-waitforframes-in-coroutine/
     
    NeatWolf and Rodolinc like this.
  10. Rarceth

    Rarceth

    Joined:
    Oct 15, 2015
    Posts:
    27
    Absolutely reviving this thread to ask question about that last answer; When you ask for the return of the Coroutine "Frames", it actually waits for the coroutine???? I had previously searched for a way to wait for coroutine to be finished and heard nothing useful!
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,283
    You don't actually have to do a StartCoroutine there, so it's even simpler:

    Code (csharp):
    1. public IEnumerator CoroutineAction()
    2. {
    3.     // do some actions here
    4.     yield return WaitFor.Frames(5); // wait for 5 frames
    5.     // do some actions after 5 frames
    6. }
    The rules are:
    - if you yield a YieldInstruction - like WaitForSeconds or a custom one - the coroutine waits until that returns false for
    keepWaiting
    - if you yield an IEnumerator or a StartCoroutine, you start that coroutine, and your current coroutine waits until the inner coroutine is done.
    - if you yield anything else, you wait a single frame.
     
    NeatWolf and Garrettec like this.
  12. Godfreyluck

    Godfreyluck

    Joined:
    Nov 4, 2018
    Posts:
    3
    Here's a question, and possible solution to the original question.

    If I use WaitForSeconds in an IEnumerator, and set the time to 0, will the coroutine start again next frame, or immediately?

    For example:

    IEnumerator ExampleCoroutine()
    {
    First();
    yield return new WaitForSeconds(0);
    Second();
    }

    Will Second() happen on the same frame as First(), or one frame later?
     
  13. asd234w4r5

    asd234w4r5

    Joined:
    Oct 4, 2012
    Posts:
    886