Menu
CoddyTech

Recap: Level Select

Part of the Building Real Games section of Coddy's Unity journey. Lesson 21 of 56.

challenge icon

Challenge

Easy

A level select screen. The build lists Menu (index 0), Level1, Level2 and Level3. The Menu has three buttons, which the Inspector puts in levelButtons of the LevelSelect script, and a Progress object. In each level the arrows walk the Player to a gold Goal, whose script (ready) calls Progress.Instance.LevelDone(level) with the level's build index.

Progress.cs:

  • Progress lasts the whole game and there is only ever one, reached as Progress.Instance, though the Menu makes a new one each time it loads.
  • unlocked is the highest level the player may play (1 at the start). LevelDone(int level) unlocks the level after it, never locks one that was open, and goes back to the Menu.

LevelSelect.cs: in Start, give each button a listener that loads its own level (the first button Level1, and so on), and make a button clickable only if its level is unlocked.

Press Play, play the levels in order and replay Level 1, then Check.

Try it yourself

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LevelSelect : MonoBehaviour
{
    public Button[] levelButtons;

    void Start()
    {
        // each button opens its level; locked levels cannot be clicked
    }
}

All lessons in Building Real Games