Lesson 7 of 9 · 25m
Building a Complete Game Loop
Connect menu, play, pause, win, lose, restart, and progression cleanly so the game is a product rather than a mechanics demo.
A mechanics demo has a player, an enemy, and a hallway. A game has a beginning, an ending, failure, restart, feedback, persistence, and a reason not to collapse when someone presses Escape at the wrong time. Finishing is not polish at the end; finishing is architecture that admits the end exists.
Model the loop as states
Even a tiny arcade game benefits from explicit state:
Boot -> MainMenu -> Playing -> Paused
|-> LevelComplete -> Playing
|-> GameOver -> MainMenuWithout states, every system improvises. UI keeps updating during pause. Enemies attack during the victory screen. Restart leaves half-reset objects behind. A state machine is boring because boring is good.
Own time explicitly
Time.timeScale = 0 is a pause hammer, not a pause design. It stops scaled time-based movement, but input, audio, UI animation, physics callbacks, and unscaled timers need deliberate decisions:
public void SetPaused(bool paused)
{
Time.timeScale = paused ? 0f : 1f;
gameplayInput.enabled = !paused;
pauseMenu.SetActive(paused);
}Use Time.unscaledDeltaTime for pause-menu animation. Decide whether music ducks, continues, or stops. Silence is also feedback.
Scene loading needs a single owner
Multiple scripts calling SceneManager.LoadScene independently creates race conditions and impossible transitions. Centralize flow:
public class GameFlow : MonoBehaviour
{
public void StartGame() => Load("Game");
public void QuitToMenu() => Load("MainMenu");
private void Load(string scene)
{
Time.timeScale = 1f;
SceneManager.LoadScene(scene);
}
}For larger games use additive scenes and loading screens. For a small complete game, simple synchronous loading can be fine if transitions and cleanup are correct.
Restart means reset everything
Restart bugs are inventory audits. What persists?
score reset
player health reset
enemy spawners reset or destroyed
object pools release active instances
audio sources stop one-shots
time scale restore
input state clear queued actions
coroutines stop or finish safelyScene reload solves many problems, but only if persistent objects and static events do not leak across the boundary.
Field notes
- Build the loop early. Discovering save and restart requirements in the final week rewrites systems you thought were done.
- Death should be readable: cause, consequence, and next action. If players ask "what happened?", feedback failed.
Was this lesson helpful?