- Home /
Coroutines not passing yield
I have an issue with a level loader I've written where the following code is not completing:
IEnumerator LoadAsync(int i) {
Debug.Log ("Loading...");
aop = Application.LoadLevelAsync(i);
aop.allowSceneActivation = false;
Debug.Log ("Post-LLA");
yield return aop;
Debug.Log ("Load done time: " + Time.time);
loading = false;
levelReady = true;
yield return new WaitForSeconds(1f);
//aop.allowSceneActivation = true; (this happens in the Ready function)
Ready ();
}
The aop variable is defined as an AsyncOperation. Some background here, I had another yield return new WaitForSeconds(1f) line preceding the yield return aop line, and it would hang on that as well. I get the debug output in the console before any yields. I'd originally started writing this as a problem with LoadLevelAsync/AdditiveAsync, but then discovered none of the yield calls were getting through. I've tried calling with both StartCoroutine(LoadAsync(i)) and ("LoadAsync", i) just to make sure there wasn't some odd change I missed.
It is being called from this function on an object tagged for DontDestroyOnLoad():
public void LoadLevel (int i) {
loading = true; levelReady = false;
Application.LoadLevel("Loading");
PercentageID pid = FindObjectOfType<PercentageID>();
if (pid != null) percentageText = pid.GetComponent<Text>();
loadContent = GameObject.Find ("LoadingContent");
Debug.Log ("Load start time: " + Time.time);
StartCoroutine ("LoadAsync", i);
}
Any ideas on what's happening? Is this a bug with the latest versions of Unity? (I was on 5.0.1 originally and now the current 5.0.2)
Answer by jctz · Jun 10, 2015 at 05:32 AM
If you're 100% sure that your object isn't being destroyed, then I would suggest that you add a 'yield return null' at the very top of LoadAsync(). This is because Application.LoadLevel() doesn't actually work right away and only loads the specified level at the end of the current frame. Without it, you're effectively doing Application.LoadLevel() and Application.LoadLevelAsync() on the same frame. Who knows how Unity will respond?
You were right, I double-checked and it was still getting destroyed. For some reason I neglected to consider that the parent transform it was a child of was not tagged to be kept between level loads, so it just removed the whole manager container. I may feel a bit dumb, but I'm glad you had me double-check, so thanks for that!
Your answer
Follow this Question
Related Questions
Collection Change During Iteration 0 Answers
Coroutines and states 1 Answer
Coroutine Not Working 2 Answers
Coroutine sequence not running properly 1 Answer
[ Coroutine: Move Next ] CPU usage 0 Answers