Why Unity docs do not use New with WaitForSeconds and yield?
I am wrapping my head around Coroutines and yield (and indirectly WaitForSeconds) and have noticed an inconsistency that initially caused me a lot of pain in sorting out why code had errors.
The main thing I'm noticing is that here, the documentation shows use of WaitForSeconds without creating a new object:
https://docs.unity3d.com/400/Documentation/ScriptReference/WaitForSeconds.html https://docs.unity3d.com/400/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Code example from the first link:
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
print (Time.time);
That however gives me compile errors. If I use this, below, I do not get compile errors:
// Prints 0
print (Time.time);
// Waits 5 seconds
yield return new WaitForSeconds (5);
// Prints 5.0
print (Time.time);
My question is WHY do the examples from official documentation result in compile errors?
Answer by AurimasBlazulionis · Mar 13, 2017 at 04:44 PM
Firstly, do you really need version 4.0 docs? I doubt it, but if you do, then it is fine.
Now the answer: it is a JavaScript syntax, you can see above the script and change to Boo if you are that kind of strange person or regular C#.