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

A question about IEnumerator

Discussion in 'Editor & General Support' started by Jacker, Nov 2, 2013.

  1. Jacker

    Jacker

    Joined:
    Jul 16, 2013
    Posts:
    3
    Hi,

    I'm a begineer, there is a puzzle to me. please see below:

    style1: void Update () {
    transform.Translate(Vector3.forward*speed*Time.deltaTime);
    }


    style2: void Start(){
    StartCoroutine(forward());
    }

    IEnumerator forward() {
    while(true){
    transform.Translate(Vector3.forward*speed*Time.deltaTime);
    yield return null;
    }
    }

    Is there something difference between these two style?
     
  2. Evil_Echo

    Evil_Echo

    Joined:
    Sep 30, 2011
    Posts:
    48
    The 2nd method runs in parallel to your main update loops. The yields in it are set to delay minimally, so that loop will move your object far more quickly.
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    That is not true! Coroutines do not run in parallel. They are executed after Update.
    http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html

    In this example, they are identical.
     
  4. gecko938

    gecko938

    Joined:
    Dec 28, 2011
    Posts:
    12
    Another difference to watch for with coroutines:
    If the game object is ever disabled, the Coroutine will be killed. If the game object is then re-enabled, the Coroutine will not resume unless you start it up again manually. That would not be an issue when moving the object within Update().
     
unityunity