Create a new method for each coroutine in C# is very boring. In Javascript I could use anonymous functoins to start a coroutine. But in C# I found no way to reproduce this code.
StartCoroutine(function() {
// ... do something
yield WaitForSeconds(1)
// .. do one more thing
}());
This question is not about migrate code from UnityScript to C#, but trying to discover a nice way to implements very asynchronous code without create a lot of functions.
Give this crazy complex example below. It is easy to implement and clear to understand a lot of “paralelism”. I can’t see any way to implement stuff like that in C# without create at least 3 functions (one for each coroutine).
StartCoroutine(function() {
// wait user to achieve level 2
while (level < 1) yield 0;
// give some time
yield WaitForSeconds(3);
// show a nice label
StartCoroutine(function () {
yield label.FadeIn();
labelShowingAudio.Play();
yield WaitForSeconds(3);
yield label.FadeOut();
label.Hide();
}());
StartCoroutine(function () {
player.anim['dance'].Play();
yield WaitForSeconds(player.anim['dance'].length);
enemy.KillPlayer();
}());
while (level < 2) yield 0;
.
.
.
}());
[Answer copied from lordofduct on Unity Forums for the sake of anyone else having trouble finding the answer buried in the comments of the answers here]