Loading level using a string created through code.

Whats up!

I have a simple question can you load a new level using a string created in a script? e.G:

var LevelToLoad : String = "";

function Update (){
if(Input.GetButtonDown("Jump")){
    LevelToLoad = "LevelOne";
    LoadLevelFunction ();
    }
}

LoadLevelFunction (){
Application.LoadLevel(LevelToLoad);
}

But it just says unknown identifier ´LevelToLoad´.

I could use some sort of loadlevel +(=) 1;

But it would be much more easy if you can fill Application.LoadLevel with a string created by the user.

Thank, you for your time.

Add ALL levels you want to Load: Build Settings-> Scenes To Build

private var levelToLoad: String="";

function Update()
{
     if(Input.GetKeyDown("space"))
    {
         LoadLevel("levelToLoad");
    }

}

function LoadLevel(p: String)
{
     Application.LoadLevel(p);
}

I can’t see anything wrong with your code, you should be able to do this. As SCape_games said, make sure you have them checked in your Build Settings so they are in fact included. But I don’t think that would account for that particular error message.

One note though: It’s common to Capitalize the First letter of a Function , and have small case for the first letter of a variableName to keep them cognitively different. Just a convention most of us follow.

You’re missing “function” in front of LoadLevelFunction declaration.