- Home /
LoadLevel*Aynsc() and Coroutine
I have a LoadScene() method within my script which looks like this,
void LoadScene(String scene)
{
async = Application.LoadLevelAdditiveAsync(scene);
StartCoroutine(SceneLoadCallback(scene));
}
where 'async' is declared as follows,
private static AsyncOperation async = null;
The Coroutine 'SceneLoadCallBack()' is defined as follows,
IEnumerator SceneLoadCallback(String scene)
{
while (!async.isDone) {
Debug.Log("Checkpoint-1")
yield return null;
Debug.Log("Checkpoint-2")
}
yield break;
Debug.Log("Checkpoint-3")
}
The method 'LoadScene()' is called directly from the 'Update()' method of my script. The level that I am trying to load gets loaded correctly, however 'Checkpoint-2' and 'Checkpoint-3' never get printed.
I am new to Unity and have just learned how to use Coroutines. If I understand it right, 'yield return null' should cause Coroutine body to resume executing (after Update() is called) at where it left. But that doesn't seems to be happening. Any insights here would be helpful.
Answer by dorpeleg · Sep 12, 2013 at 07:06 AM
You should run the method 'LoadScene()' from 'Start()' and not from 'Update()'.
Coroutines do not need to be in 'Update' in order to run.
Answer by raimon.massanet · Sep 12, 2013 at 07:40 AM
We experienced a similar issue whith LoadLevelAsync. It seems the isDone property does not work as expected and is never set to true (unless it has been fixed in recent releases). Instead we used the progress property and checked when it was over 0.91f. I know it's not a very ellegant solution, but it works.
Another approach you might want to try is to set the allowSceneActivation to false after calling LoadLevelAsync, then do whatever you want to do while the scene is loading, and set allowSceneActivation back to true when you have finished. That way you don't need to control when the scene has finished loading because it will automatically load when it's ready.
Finally, yield break, breaks the flow of the coroutine, which means that anything after that line will never get executed.
From my experience, progress also doesn't work as expected.
Unless you use them both:
if (!async.isDone) {
Debug.Log(async.progress);
}
It's a known bug (at least for users, not sure if unity is aware).
We used the progress property as described above (check if progress is above 0.89f, I wrote it wrong before) and the solution worked for us. It seems as if the last 0.1f is never completed unless the scene is allowed to load.
It seems as if the last 0.1f is never completed unless the scene is allowed to load.
Interesting remark... will look out for that.
Based on the answer by raimon.massanet, I have now changed my SceneLoadCallBack() method to look as follows,
IEnumerator SceneLoadCallback(String scene)
{
while (!(async.progress>0.89f)) {
Debug.Log("Progress: "+async.progress);
yield return null;
}
Debug.Log("Checkpoint-3");
yield break;
}
Still, the 'Checkpoint-3' statement never gets printed. The progress statement gets printed just once with a value of 0.
I have also tried setting a breakpoint inside the while loop, on the yield line, and when it gets executed (just once) the watch on the 'async' variable says '$$anonymous$$ identifier'. I am not sure if that has anything to do with the behavior I am seeing, but I cannot explain why that may be happening as 'async' is a static variable.
Answer by Weendie-Games · Jan 17, 2018 at 11:10 AM
"AsyncOperation.isDone will never be true if AsyncOperation.allowSceneActivation is false. Progress will stop at around 0.9f."
Check this answer: https://answers.unity.com/answers/956839/view.html
Your answer
Follow this Question
Related Questions
Can I use WWW in Start()? 1 Answer
Coroutine not working properly on Android (C#) 0 Answers
Problem detection collider with coroutine 1 Answer
Get static var inside coroutine (C#) 1 Answer
Sequential Coroutines sometimes halt 1 Answer