Coroutines vs Update/FixedUpdate/LateUpdate

A friend on twitter, @pixelplacement recently pointed out that he never uses Update anymore, instead preferring co-routines for everything, citing that it makes state management a dream (among other advantages).

Firstly, could someone explain how to replace “Update” with an equivalent coroutine, and variously how you sequence things under this paradigm, and secondly, are there any thoughts (Unity engine developers especially) as to whether this is a good way to go?

If you want to literally replace Update with a coroutine, then do this:

function Start () {
    while (true) {
        // do stuff here that you'd normally do in Update
        yield;
    }
}

If that’s all you’re doing, then it has no advantage over Update. I wouldn’t say never use Update; it’s good for things which just do the same thing every frame. Coroutines are good for when you want to schedule events. For example, if you have an object moving forward, but you want it to stop for a second when you hit a key, you would do something like this if you use Update:

private var wait = false;
private var timer : float;

function Update () {
	if (!wait) {
		transform.Translate(Vector3.forward * Time.deltaTime);
	}
	else if (Time.time >= timer) {
		wait = false;
	}
	if (Input.anyKeyDown) {
		wait = true;
		timer = Time.time + 1.0;
	}
}

The thing is, that code isn’t doing the same thing every frame. Sometimes it’s doing one thing, sometimes another. So let’s rewrite that as a coroutine:

function Start () {
    while (true) {
        transform.Translate(Vector3.forward * Time.deltaTime);
        if (Input.anyKeyDown) {
            yield WaitForSeconds(1.0);
        }
        yield;
    }
}

As you can see, there are no more global variables and slightly convoluted if/then logic. The code is shorter and basically self-documenting: “If you press any key, wait for a second.” And this is just a very simple example…as states get more complex, coroutines get more powerful. See here for a somewhat more complex example.

Co-routines are fantastic for performance. Sometimes you have to perform actions every frames, often you don’t. Co-routines are functions you can call every few seconds to do stuff like AI. I’ve never really considered it for state management but I’m pretty new to coding and tend to use booleans for that :wink:

Here is the overview for co-routines:

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

and the script ref:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html