Any Level Select Scripts Out there?

I’m building a game with like 30 levels and I wonder if anyone has ever built a level selecting script which I could use. Or, does anyone know a tutorial which I could follow?

My level selecting menu would be a separate scene and you would click GUI Buttons to select a level.

How would you check if the person beat the previous level to unlock a new level? And where would you save the data?

Any help is appreciated!

If the levels unlock sequentially then all you have to do is save an interger in PlayerPrefs.
int nomRows = 5;
int nomCols = 6;
int n = 0;
int maxAllowedLevel = PlayerPrefs.GetInt(“MaxLevel”);
for(int i=0; i<nomRows; i++)
{
for(int j=0;j<nomCols; j++)
{
GUI.enabled = n < maxAllowedLevel;
if(GUI.Button(new Rect(i40,j30,50,20),n.ToString()))
{
Application.LoadLevel( n );
}
n++;
}
}

Though I’ve heard it many times, I’m not very familiar with PlayerPrefs.

I know it’s for saving data though. Could you explain more of it to me?

EDIT: With the script you provided, would I just insert it and add it to an empty gameobject? Because it is giving me errors.

Anyone?

Depends what you mean by “save the data”?

Use PlayerPrefs for things like progress/highscores, anything that you want to keep when you quit application.
PlayerPrefs.SetString(“PlayerName”, playerName); Would remember for next time the name of the player, etc.

For keeping data between Scenes, have a GameObject with a DontDestroyOnLoad script. This will hold variables independent of whatever scene you load.

In my game, I would like to keep the data for next time (save the progress). So I think I would use PlayerPrefs.

How would I save the level number and get the level number so I know which level to unlock?

//This will store the current level
//currentLevel being an int.
PlayerPrefs.SetInt(“level_index”, currentLevel);

//This will store the next level
PlayerPrefs.SetInt(“level_index”, currentLevel+1);

//Get the level to be loaded
//indexLevel being an int.
indexLevel= PlayerPrefs.GetInt(“level_index”);

Application.LoadLevel(indexLevel);
//You must know the scene index if you use GetInt(), you can also load scenes with their names.
In that case you’ll pass the scene name as a string.

string sceneName = “second level”;

Application.LoadLevel(sceneName.ToString);
//or
Application.LoadLevel(“second level”);

Thanks.