Load Level Help

Ok so I have been trying to make a script that will load a level. The trick is I want it to be reusable instead of having the same script, but having to multiply it just to change one thing.

How I have been doing it is creating the variable “public string level;” and then making an if statement, “if(Input.GetKeyDown(Keycode.KeypadEnter))” and then under that have “Application.LoadLevel( level );”

Then after that I would just type the name of the level I want to load in the editor, but it won’t work. Any help?

Populate your scene list in Build Settings window.

Then use

Application.LoadLevel( level ); //level is int equal to level index

where level is an int, not a String. The index (int) is the same as what appears in the build settings window. 0 is default scene. 1 is next level etc.

You have declared

public string level;

which requires

Application.LoadLevel( "levelname" );


public string level = "levelname";

as it’s a String.

I would say that int is easier for level-hopping. But either way, you should populate your scene list.