- Home /
Application LoadLevel() not working as expected
I have a quick prototype that I've whipped up in Unity but I'm having issues with scene transitions. I have a Game Scene and a Start Scene, akin to those flash game loading screens that have instructions or whatever. The issue I'm having is when I use Application.LoadLevel to load the game scene from the starting scene, game objects don't seem to be initialized and my game crashes with null exceptions. When I run the game scene from the editor everything works fine. What am I doing wrong?
My code pretty much is this:
if (GUI.Button(new Rect(100.0f, 50.0f, 100.0f, 50.0f), "Play Defending"))
{
GameController.clientColour_ = PlayerEnum.Blue;
Application.LoadLevel("gameScene");
}
if (GUI.Button(new Rect(210.0f, 50.0f, 100.0f, 50.0f), "Play Attacking"))
{
GameController.clientColour_ = PlayerEnum.Red;
Application.LoadLevel("gameScene");
}
So you're trying to set some parameters of GameController that are used in the next scene? How is GameController defined?
I think you'll have to create some kind of persistence to keep the game objects around through multiple scenes [if I'm understanding your question correctly]
Answer by logang · Apr 02, 2013 at 05:40 PM
Here try this:
if (GUI.Button(new Rect(100.0f, 50.0f, 100.0f, 50.0f), "Play Defending"))
{
GameController.clientColour_ = PlayerEnum.Blue;
Application.LoadLevel(1);
}
if (GUI.Button(new Rect(210.0f, 50.0f, 100.0f, 50.0f), "Play Attacking"))
{
GameController.clientColour_ = PlayerEnum.Red;
Application.LoadLevel(1); //in build settings look at the scene you want to load to and set the number of the scene to that loadlevel
}
that should work
All that does is change the level to be indexed by its build number rather than its name. That wont solve my issue. It loads the right level, but nothing seems to be initialized.
I've got the same / simliar problem. The first loading of my main menu is working, but if I am (after playing) want to go pack to the main menu, I only get a blue screen. Scripts are still working, so the main camera is initialized (I've attached a script to it, which checks Input), but I can't find it through GameObject.Find($$anonymous$$ain Camera). [...] Camera.main.gameobject. [...] . In both cases I only get a NullRefereneException. That is only happening as Standalone, in the editor everything is working perfectly. Also some days ago nearly the same scene was working without any problems. I only added one gameobject.
EDIT: Some builds later I just tried it again and now it is working again.. curious bug..
Answer by cmpgk1024 · Aug 08, 2013 at 11:32 PM
To have objects persist between scenes you need to use `Application.LoadLevelAdditive`. Your null reference exceptions are likely coming from GameController not persisting.
I think you don't understand the question. That is not the problem.
Answer by GroutchoGG · Apr 02, 2015 at 05:43 AM
You have to wait till the next frame before you can access the loaded level objects and properties.
You can do that in a coroutine :
Enumerator LoadLevelRoutine(string levelName)
{
Application.LoadLevel(levelName);
yield return null; // let a frame pass
// now you can access the scene objects and properties
}
Then, in your code, just do :
StartCoroutine(LoadLevelRoutine("my level"));
Your answer