Trying to understand Coroutines

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

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.

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:

IEnumerator Delay()
{
    Debug.Log("Started coroutine.");
    yield return new WaitForSeconds(3);
    Debug.Log("Ended coroutine");
}

void Start()
{
    StartCoroutine("Delay");
}

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:

IEnumerator Delay()
{
    yield return new WaitForSeconds(3);
}

void Start()
{
    Debug.Log("Started coroutine.");
    StartCoroutine("Delay");
    Debug.Log("Ended coroutine");
}

In Unityscript tho its a bit simpler:

function Start()
{
    Debug.Log("Started coroutine.");
    yield WaitForSeconds(3);
    Debug.Log("Ended coroutine");
}

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! :slight_smile:

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)

function SomeCoroutine () : IEnumerator {
    yield WaitForSeconds (1);
}

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.

int timer=0;
bool 1second=false;
bool 2seconds=false;
bool 3seconds=false;

void Update () {
    timer += Time.deltaTime;
    if (!1second && timer > 1) {
        1second = true;
        Debug.Log ("Start 1 second event");
    } else if (!2seconds && timer > 2) {
        2seconds = true;
        Debug.Log ("Start 2 second event");
    } else if (!3seconds && timer > 3) {
        3seconds = true;
        Debug.Log ("Start 3 second event");
    }
}
IEnumerator Start () {
    while (true) {
        yield return new WaitForSeconds (1);
        Debug.Log ("Start 1 second event");
        yield return new WaitForSeconds (1);
        Debug.Log ("Start 2 second event");
        yield return new WaitForSeconds (1);
        Debug.Log ("Start 3 second event");
    }
}

Don’t use StartCoroutine (string) unless you plan on using StopCoroutine. In all other cases it is better to use StartCoroutine (IEnumerator)

1 Like

this tutorial explains them pretty good from my pov.

And this aswell :wink:

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.