Coroutines in Unity3d (C# version)

Coroutines in C# work the same way that they do in Javascript (UnityScript), the only difference is that they require more typing (they have a slightly more complicated syntax). You should see the blog post on Javascript coroutines first.

Thanks Seth for sharing these basics. Definitely useful to those looking to learn.

One note: I had heard that returning null is ever so slightly faster than returning 0 when yielding in a coroutine due to the allocation for creating a new variable 🙂

http://cjcurrie.net/ cjcurrie

So how do you create a coroutine that returns a value at the end?

http://rockingtech.blogspot.com Robert

thanks for sharing the code for this. i really needed to know and i will use a slightly modified one in my application. What if i wanted to return more than one values. Is there any way i can do it.

Richard Hensman

Hi,

So according to Unity Docs, you can’t use yield in an Update statement, but you can start a CoRoutine that does.

I am somewhat confused by this. In my code I have two objects. Object 1 needs to make two calls to Object 2. Object 2, in each case, starts an animation, waits X seconds (local member of Object 2), then returns to Object 1. So I think Object 2 code looks like: