Search Unity

Any Level Select Scripts Out there?

Discussion in 'General Discussion' started by MikezNesh, Oct 31, 2010.

  1. MikezNesh

    MikezNesh

    Joined:
    Jun 18, 2010
    Posts:
    37
    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!
     
  2. Aiursrage2k

    Aiursrage2k

    Joined:
    Nov 1, 2009
    Posts:
    4,835
    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(i*40,j*30,50,20),n.ToString()))
    {
    Application.LoadLevel( n );
    }
    n++;
    }
    }
     
    Last edited: Oct 31, 2010
  3. MikezNesh

    MikezNesh

    Joined:
    Jun 18, 2010
    Posts:
    37
    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.
     
    Last edited: Oct 31, 2010
  4. MikezNesh

    MikezNesh

    Joined:
    Jun 18, 2010
    Posts:
    37
    Anyone?
     
  5. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    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.
    http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html
     
  6. MikezNesh

    MikezNesh

    Joined:
    Jun 18, 2010
    Posts:
    37
    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?
     
  7. tatoforever

    tatoforever

    Joined:
    Apr 16, 2009
    Posts:
    4,367
    //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");
     
  8. MikezNesh

    MikezNesh

    Joined:
    Jun 18, 2010
    Posts:
    37
    Thanks.