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

Trying to understand Coroutines

Discussion in 'Scripting' started by loserman778, Jul 17, 2014.

  1. loserman778

    loserman778

    Joined:
    Feb 21, 2014
    Posts:
    2
    Hello everyone, I'm fairly new to working with Unity and I've been working through a few tutorial series to get me started. Almost every series I've gone through uses a Co-routine and every time the tutorial tries to explain what it is, but i still never get it. I found this article, which is more detailed than most tutorials, but i still don't understand it. If you look at one of the examples on the link, the last one under 'Count Seconds' I'm not quite sure why you even need the IENumerator, couldn't you do the same thing, almost as easily, without it?

    If anyone could point me in the right direction so i can try to further my understanding of co-routines, that would be awesome. Thanks guys,


    -Loserman778
     
  2. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    I don't know why you have to use a coroutine in C#, but in UnityScript/JavaScript you can simply use yield WaitForSeconds(seconds) instead of creating an IEnumerator.
     
  3. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    That's just how it works in C#.

    In C# the coroutine is the IEnumerator.

    So if you wanted to lets say, wait 3 seconds, it would be:

    Code (CSharp):
    1. IEnumerator Delay()
    2. {
    3.     Debug.Log("Started coroutine.");
    4.     yield return new WaitForSeconds(3);
    5.     Debug.Log("Ended coroutine");
    6. }
    7.  
    8. void Start()
    9. {
    10.     StartCoroutine("Delay");
    11. }
    Also I don't think you can run it inside Update, or it will be started every frame and never end. You just need to call it once.

    EDIT:

    It would also work if you put it like this:

    Code (CSharp):
    1. IEnumerator Delay()
    2. {
    3.     yield return new WaitForSeconds(3);
    4. }
    5.  
    6. void Start()
    7. {
    8.     Debug.Log("Started coroutine.");
    9.     StartCoroutine("Delay");
    10.     Debug.Log("Ended coroutine");
    11. }
    In Unityscript tho its a bit simpler:

    Code (JavaScript):
    1. function Start()
    2. {
    3.     Debug.Log("Started coroutine.");
    4.     yield WaitForSeconds(3);
    5.     Debug.Log("Ended coroutine");
    6. }
    However, stick to C#, most tutorials and assets you find will probably be in C#. Unityscript (Unity's javascript) is slowly phasing out.

    You should go through the Unity's official tutorials here, its all well explained, you just might need to pause or replay every now and then to picture that. xD

    The Coroutines tutorial specifically.

    Good luck! :)
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Not quite, if you were to try that in Update you wouldn't be able to use the yield command to wait until the next frame. Because of that, the counting of 1 second would happen in a single frame.

    Also, because of this (quoted from the article)
    In UnityScript/JavaScript the return type is IEnumerator, it's just hidden from you (unless you declare it explicitly)
    Code (csharp):
    1. function SomeCoroutine () : IEnumerator {
    2.     yield WaitForSeconds (1);
    3. }
    WaitForSeconds is also used in C#, but using that would have skipped a part of the lesson.



    Coroutines are functions that take place over multiple frames. You never need a coroutine, but living without them would be very difficult.

    Here are two scripts you can compare, one uses a coroutine. One does not.
    Code (csharp):
    1.  
    2. int timer=0;
    3. bool 1second=false;
    4. bool 2seconds=false;
    5. bool 3seconds=false;
    6.  
    7. void Update () {
    8.     timer += Time.deltaTime;
    9.     if (!1second && timer > 1) {
    10.         1second = true;
    11.         Debug.Log ("Start 1 second event");
    12.     } else if (!2seconds && timer > 2) {
    13.         2seconds = true;
    14.         Debug.Log ("Start 2 second event");
    15.     } else if (!3seconds && timer > 3) {
    16.         3seconds = true;
    17.         Debug.Log ("Start 3 second event");
    18.     }
    19. }
    20.  
    Code (csharp):
    1.  
    2. IEnumerator Start () {
    3.     while (true) {
    4.         yield return new WaitForSeconds (1);
    5.         Debug.Log ("Start 1 second event");
    6.         yield return new WaitForSeconds (1);
    7.         Debug.Log ("Start 2 second event");
    8.         yield return new WaitForSeconds (1);
    9.         Debug.Log ("Start 3 second event");
    10.     }
    11. }
    12.  


    Don't use StartCoroutine (string) unless you plan on using StopCoroutine. In all other cases it is better to use StartCoroutine (IEnumerator)
     
    TRALLALAL likes this.
  5. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
  6. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
  7. loserman778

    loserman778

    Joined:
    Feb 21, 2014
    Posts:
    2
    Thanks for the info all, I'm going to look through all the resources you guys posted and I'm going to see if i can grasp Coroutines.