Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. 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

Use IEnumerator as delay

Discussion in 'Scripting' started by gamer123454321, Aug 2, 2020.

  1. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    18
    Hi! I just wonder if I keep using StartCoroutine/ IEnumerator as delay for play animation then run other function.
    Will the timing different in different mobile or computer?
    for example...
    if I fade in for a scene

    img.GetComponent<Animator>().SetBool("fadeIn", true);
    yield return new WaitForSeconds(1f);
    then do something else.
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,097
    WaitForSeconds works fine for about 1/4th of a second or more. 1 second is 1 second on any platform.

    The main problem is with
    yield return null
    ;. That waits until next frame, which is obviously twice as long at 30FPS over 60 (and even worse if the editor is running at 100+). The other problem is with very small times. Unity _only_ waits in frames. If you tell it to wait for 1/50th of a second, all Unity can do at 60FPS is wait 2 frames, which is really 1/30th of a second. You can use both of those things, but you have to do extra work to keep the times the same.
     
    Bunny83 likes this.
  3. gamer123454321

    gamer123454321

    Joined:
    Mar 25, 2020
    Posts:
    18
unityunity