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:
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:
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:
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 ;)
Here is the overview for co-routines:
and the script ref:
answered May 20 '11 at 08:44 AM