Recap: Level Select
Part of the Building Real Games section of Coddy's Unity journey. Lesson 21 of 56.
Challenge
EasyA 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. unlockedis 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
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap