Passing a variable to a scene

Trying to create a generic loading screen scene that will load different levels based on a variable.

How can i pass a variable with Application.LoadLevel?

My advice, is to create a GUI element to handle the loading screen, instead of a scene. Then, check out the level loading functions in:

Sorry, there is also a way to handle this via a standard component. You can make a persistent component via the DontDestroyOnLoad mutator.

var selectedLevel : int = 0;
if(GUILayout.Button("Level 1"))
{
   selectedLevel = 1;
}
if(GUILayout.Button("Level 2"))
{
   selectedLevel = 2;
}
if(GUILayout.Button("Load selected level))
{
   Application.LoadLevel(selectedLevel);
}

or you could also simply do :

if(GUILayout.Button("Level 1"))
{
   Application.LoadLevel(1);
}

and so on.

That’s not precisely what he is asking for. He’s looking to create a scene that handles all loading scenes, move the player to the loading scene, and then via a variable, load a specific level.

I still say the best way to handle this, would be to create a component that handles the loading screen GUI and jacks into the load level business on the fly, and then adds the component to the current level when needed.

@appels
I don’t want to load the level directly because it takes too long to load. Want to use a loading screen to load another level.

@TerXIII
Ended up creating an empty game object called SceneToLoad with that don’t destroy function that just stores the name of the scene to load.

So the logic I have now is:

  1. Set “scene to load” variable in that persistent object.
  2. Application.LoadScene(“loadingscreen”)
  3. Loading screen scene loads in milliseconds.
  4. Call Application.LoadLevelAsync(SceneToLoad.scene)
  5. Update loading bar in loading scene.
  6. New scene is loaded when its ready.

Thanks

My loading screen scene has some animations, images, etc though. If i just had it as a script only, i would need to hide the current scene somehow.

Check out this thread:

http://forum.unity3d.com/viewtopic.php?p=288984

Create an entity that will be in the scene that the player is leaving, then tell it to not destroy on level load, and then change some arbitrary value in it. Then, when the loading level loads, you locate the object, grab the variable, and then destroy the object manually. Now you have your stored variable.