system
1
Why C# is lacking Javascript features in Unity.. How can I use the similar coding like below in C#?
print ("Starting " + Time.time);
// Start function WaitAndPrint as a coroutine
yield WaitAndPrint();
print ("Done " + Time.time);
function WaitAndPrint () {
// suspend execution for 5 seconds
yield WaitForSeconds (5);
print ("WaitAndPrint "+ Time.time);
}
when I try to call yield in C#, it always say that feature is not supported in C#..
Mike_3
2
This should do it, added a couple comments to describe what's needed:
Debug.Log ("Starting " + Time.time);
// Start function WaitAndPrint as a coroutine
//you need to explicitly tell it to start the coroutine - javascript does this implicitly (most of the time)
yield return StartCoroutine(WaitAndPrint());
Debug.Log ("Done " + Time.time);
//the function return type needs to be IEnumerator (javascript does this implicitly) - you'll need to have using System.Collections; up at the top of your code
IEnumerator WaitAndPrint ()
{
// suspend execution for 5 seconds
yield return new WaitForSeconds (5);
Debug.Log ("WaitAndPrint "+ Time.time);
}
Tetrad
4
`yield` isn't just for coroutines either.
Try doing this in Javascript.
IEnumerator CardinalDirections()
{
yield return "North";
yield return "South";
yield return "East";
yield return "West";
}
// another function somewhere
foreach( string direction in CardinalDirections() )
{
Debug.Log( direction );
}
I wrote a tutorial on this a while back. liiiiiink
Cheers
==