- Home /
LoadLevelAsync - Stops Randomly?
I'm using 'Application.LoadLevelAsync' to load my game level from my main menu.
But about 3 out of 5 tries, it will just ... stop, and freeze! (This only happens when built and running on my iOS devices - doesn't happen within the editor).
It doesn't crash, nor is Xcode throwing any errors, it just simply stops, mainly at around 80% level loaded. Although my loading music is still playing.
Can anyone explain this?
This is my function thats called when loading a new scene:
function LaunchGame(players : String, game : String, table : String){
print("[MC] Launching Game!");
PlayerPrefs.SetInt("background_colour", backgroundRandom);
PlayerPrefs.SetString("player_type", players);
PlayerPrefs.SetString("game_type", game);
PlayerPrefs.SetString("table_type", null);
yield WaitForSeconds(1.5);
print("[MC] Loading...");
var async = Application.LoadLevelAsync(1);
while (!async.isDone) {
print ("%: " + async.progress);
yield;
}
}
Thanks.
Stupid question, but are there any errors shown in the console that originate from the scene that is being loaded? They may not necessarily stop things from "working" in the editor. I've had this happen in the past so just thought I'd check.
The only error I get right before the scene is loaded is a 'Broadcast $$anonymous$$essage has no receiver'. I'll patch that up and see if it stops. This warning comes up all the time, yet my scene only loads the majority of the time. So I doubt it is that, but - better to be safe than sorry.
You must be sending a message somewhere in a script, that has a null receiver. $$anonymous$$aybe. But yeah, I'd definitely clean that up first. $$anonymous$$ake sure there are no errors in the editor whatsoever, and then see if it still crashes on the device.
Answer by oliver-jones · Feb 20, 2014 at 04:21 PM
I couldn't find the problem exactly, but I personally think it gets stuck when trying to load the scene within the background and then once loaded, jump right into the new scene.
So what I did was made the scene load about 2 seconds after the Main Menu has loaded, but DON'T launch into it. I only launch into it when the user clicks play.
This benefits me in two ways:
1) My game never crashes during loading
2) When the player clicks play, the scene is much quicker to load in, as the majority of it has already loaded.
I do this by calling this function when I want to load the scene, but not launch it (so call it within Start)
var async : AsyncOperation;
function LoadGameInBackground(){
async = Application.LoadLevelAsync(1);
async.allowSceneActivation = false;
while(async.progress < 0.9){
print(async.progress + "%");
yield;
}
print("Level loaded in background - waiting for launch prompt");
}
Then when the user clicks play, this function is then called, which activates the scene:
function LaunchGame(players : String, game : String, table : String){
print("[MC - Launch] Saving game types");
PlayerPrefs.SetInt("background_colour", backgroundRandom);
PlayerPrefs.SetString("player_type", players); //solo, host, join, passngo
PlayerPrefs.SetString("game_type", game);
PlayerPrefs.SetString("table_type", null);
yield WaitForSeconds(0.5);
print("[MC - Launch Active] Activating new scene");
async.allowSceneActivation = true;
}