Standalone build fails Application.LoadLevel() and Freezes
Hi there again!
I've tracked down another bug of a line that worked fine in editor and, for some reason, it does it wrong in the build.
Here's the chunk of code from the scene named VeryFirstSceneEver, I'll provide an overview about what's happening nearby it so you can have an idea:
/// <summary>
/// Moves to Quit OR ProtoMenu!
/// </summary>
void MoveOn(){
//Once rendering is over...
if(textBehaviour.Rendered){
//If user refuses...
if(responsibilityRefused){
Application.Quit();
}
else{
//Go cat go!
if(Input.anyKeyDown){
Application.LoadLevel("ProtoMenu");
// StartCoroutine("LoadNext");
}
}
}
}
// IEnumerator LoadNext(){
//
// Debug.Log("Trying to load ProtoMenu, hope this works");
//
// yield return new WaitForSeconds(1f);
//
// line above is the last thing that gets processed, we never reach the one below.
// Application.LoadLevel("ProtoMenu");
// Debug.Log("The call is passed, let's see the effects!");
// }
I've tested with Debugs both from editor and from build, the results are:
Editor loads the scene called ProtoMenu every time, both with the Coroutine method ( that I dislike) and the straight one.
Build fails at loading ProtoMenu, and stops giving me feedback as soon as Input.AnyKeyDown is true ( I placed inside it a Debug and I never heard of it), or as soon as yield returns with the coroutine method... Or I should say as soon as 1 second from the start of the coroutine is through.
Now a bonus thing: Even if I can't leave this scene without freezing, before I call Application.LoadLevel() I create a couple of Directories against which the Preloading Scene of Unity does this check:
if(Directory.Exists(Application.persistentDataPath + "/Scenes Directory")){
Application.LoadLevel("ProtoMenu");
}
else{
Application.LoadLevel("VeryFirstSceneEver");
}
Since those Directories are there, the next time I launch the Build, the first condition is used.
And the first condition loads ProtoMenu scene without a line of warning.
But then a similar pattern occurs: I have to load a 3rd scene from the 2nd scene loaded since game's launch.
Guess what? the game freezes for each once again.
That's what makes me believe that something is jamming with Application.LoadLevel();
Thanks a bunch for any suggestions!
Answer by esitoinatteso · Nov 11, 2015 at 02:43 PM
Hi there! Got beefy news: I solved the problem with a workaround.
I'd like to know what was wrong in the first place though, if one is experienced enough about what's going on under the hood and has some spare time to enlighten me I'd be grateful!
So...
IF your build freezes and doesn't load scenes via Application.LoadLevel(); you make a new scene with an empty game object.
You attach to that object a script like this:
using UnityEngine;
using System.Collections;
public class LoaderOfScenes : MonoBehaviour {
// Use this for initialization
void Awake () {
//Just load a specific scene...
Application.LoadLevel(References.SceneToLoad == null ? "<Put your Default Scene name here" : References.SceneToLoad);
}
// Update is called once per frame
void Update () {
}
}
Then you need another class to store the string variable used to pick the proper scene.
I use References, which is just a plain public static class stored inside the folder of my scripts. The code for the string is this:
static string sceneToLoad;
public static string SceneToLoad{
get{
return sceneToLoad;
}
set{
sceneToLoad = value;
}
}
So, nothing to fancy.
When I run a build that, instead of using Application.LoadLevel(< the scene I need>); asap, loads the one where I saved the empty game object to which LoaderOfScenes is attached... somehow everything works.
I'm clueless about why this exactly happens.
This solution not work for me... XD In editor is all ok, but in build the application crash when load a new scene.