- Home /
yield in C# doesn't work, not event the sample code?
I copied this code straight from reference: (http://unity3d.com/support/documentation/ScriptReference/index.Writing_Scripts_in_Csharp.html)
using UnityEngine; using System.Collections; public class PauseScript : MonoBehaviour { public void Awake() { Do(); print("This is printed immediately"); }
IEnumerator Do()
{
print("Do now");
yield return new WaitForSeconds(2);
print("Do 2 seconds later");
}
}
I attached it to a cube prefrab and run it, mysteriously the line inside Do() never get executed... Then I tried the java version of the sample code, that worked fine...
Do (); print ("This is printed immediately");
function Do () { print("Do now");
yield WaitForSeconds (2);
print("Do 2 seconds later");
}
Could it be any setting in unity that I missed?? Any ideas??
Thanks guys..
Answer by Mike 3 · Feb 09, 2011 at 12:58 AM
You need to use StartCoroutine(Do()); when using c#, unlike js where it is optional
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html
Thanks so much for helping!! Took me forever and I couldn't figure it out..
Then guess the reference page needs a update: http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
None of the C# code there are called by StartCoroutine.
Indeed, that's pretty bad. The last example does it, but the rest need bug reporting