- Home /
Why do I get error when using yield?
I'm trying to create a loading bar for loading the next level/scene. I'm following unity3d's scripting manual but i get and error. here is the code:
var loadLevel:boolean = false; var async:AsyncOperation;
function OnGUI(){ if(GUI.Button(Rect(0,0,100,50),"LoadLevel 1")){ loadLevel = true; } } function Update(){ if(loadLevel){ async = Application.LoadLevelAsync ("Level_01"); yield async; if(async.isDone){ print ("Loading complete"); } } }
The error is: "Script error: Update() can not be a coroutine." when i remove the yield the error is gone but the result is not what i expect. It prints nothing:( ?
Answer by Eric5h5 · Oct 06, 2010 at 11:17 AM
Update runs every frame without exception and cannot be a coroutine. (Neither can OnGUI.) You won't find any example code in the scripting manual that has yield in Update. Instead of running checks every frame in Update, just call a coroutine when the button is pressed.
Your answer
Follow this Question
Related Questions
Update() can not be a coroutine. 2 Answers
Loading screen with LoadLevelAsync 0 Answers
Delay between animations 1 Answer
C# yield error 3 Answers
yield on a www never completes 10 Answers