- Home /
LoadLevelAsync Causes Unity to Crash?
I'm trying ot use LoadLevelAsync for the first time, in Unity Pro 4.1.5f1. The following script in an empty scene, attached to an empty GameObject, causes the Unity editor itself to hang, every time.
Does this happen for other people? What am I doing wrong? This looks OK to me...
using UnityEngine;
using System.Collections;
public class SceneLoader : MonoBehaviour {
string targetScene = "MainGame";
AsyncOperation asyncOp;
IEnumerator Start() {
// Load and wait
asyncOp = Application.LoadLevelAsync(targetScene);
asyncOp.allowSceneActivation = false;
yield return asyncOp;
Debug.Log("Load complete!");
}
void Update () {
if (asyncOp.isDone) {
Debug.LogError("Done Loading");
}
}
void OnGui() {
if (asyncOp != null) {
GUI.Box(new Rect(0, Screen.height - 40, asyncOp.progress * Screen.width, 40), "");
}
}
}
It seems without the asyncOp.allowSceneActivation = false;
line, it works. I wanted to start a fade transition on load complete, so I don't want it to swap scenes for me.
It probably crashed because you caused it to have some sort of infinite loop. Does "SceneLoader" possibly exist in the other scene as well?
Thanks for the reply. It doesn't exist in other scenes. The error also only occurs when I try to use Async loading on a large scene. It works fine with smaller scenes and with large scenes non-asynchronously. Unity's async loader seems to just die.
Do you have a crash report? This may be a Unity bug
I sent one to Unity when it crashed before. No reply yet.
They should be on the case. $$anonymous$$eanwhile can you also post the error report here?
Someone might come to a solution quicker than them :)
Answer by InfiniBuzz · Jul 05, 2013 at 09:57 AM
Remove the
asyncOp.allowSceneActivation = false;
this does load the level but never activate your scene (actually finish loading). You cannot abort a loading operation since the objects in the current scene are being unloaded. Remove the line or set allowSceneActivation to true somewhere.
asyncOp.allowSceneActivation = true;
To have a fading in the new scene you have to setup the overlay on the new (the loaded) scene and then start the fading from there. It is not possible to finish the scene loading without allowSceneActivation (this blocks the loading at about 90% if I remember right).
Hope you understand me and it helps ;)
edit: the fading like you do it now, does it not work without allowSceneActivation = false; ?
Your answer
Follow this Question
Related Questions
Loading Scene in between two scene for web player. 0 Answers
LoadSceneOnClicks.LoadAsynchronously(int)': not all code paths return a value 1 Answer
Application.LoadLevelAsync() causes Unity crash? 2 Answers
LoadLevelAsync and load data before allowSceneActivation 1 Answer
iPad crashes on load level 2 Answers