Menu
CoddyTech

Level Clear

Lesson 11 of 16 in Coddy's Unity Game: Brick Breaker course.

A game with levels needs a way to write them down. Text is the easiest: one character per brick, one string per row. In this game a digit is a brick with that many hits and a dot is a gap, and a level's rows go in one string, split by /, so a level fits on one line of the Inspector. Split('/') cuts the string into an array of rows. A garden planted from text:

string layout = "t.t.t/.ttt.";
string[] rows = layout.Split('/');
for (int row = 0; row < rows.Length; row++)
    for (int col = 0; col < rows[row].Length; col++)
        if (rows[row][col] == 't') Instantiate(treePrefab, new Vector3(col, 0f, -row), Quaternion.identity);

A digit character is not its number: '3' is a char whose code is 51. The ten digits have codes in order, so taking away '0' (code 48) gives the number: '3' - '0' is the int 3.

A level is cleared when its last brick breaks. Count: add one for every brick built, take one away for every brick broken, and at 0 the level is done. Counting the bricks still in the scene does not work at that moment, because Destroy waits for the end of the frame, so the brick that is breaking is still there.

challenge icon

Challenge

Medium

Build the bricks from text. The scene fills levels with three layouts; the first is the wall you have now.

  1. In GameManager, remove rows, columns and rowHits. Add public string[] levels, public Ball ball (the scene links the Ball), an int level and an int bricksLeft.
  2. Replace BuildWall() with void BuildLevel(int index): it stores index in level and splits levels[index] into rows at /. For every digit c it builds a brick with c - '0' hits and adds one to bricksLeft; dots stay empty. The rows are centered and spaced as before. Start() calls BuildLevel(0).
  3. In BrickBroken, take one from bricksLeft. At 0, put the ball back on the paddle and build the next level; after the last level comes the first again.

Press Play and clear a level if you can, then Check.

Try it yourself

using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public Brick brickPrefab;
    public Transform wall;
    public int rows = 5;
    public int columns = 9;
    public float spacingX = 1.75f;
    public float spacingY = 0.75f;
    public float topY = 6f;
    public int[] rowHits = { 2, 1, 1, 1, 1 };
    public int lives = 3;
    public TMP_Text scoreText;
    public TMP_Text livesText;

    int score;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        BuildWall();
        ShowHud();
    }

    void BuildWall()
    {
        float left = -(columns - 1) * spacingX / 2f;
        for (int row = 0; row < rows; row++)
        {
            for (int col = 0; col < columns; col++)
            {
                Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
                Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
                brick.SetHits(rowHits[row]);
            }
        }
    }

    public void BrickBroken(Brick brick)
    {
        score += brick.points;
        ShowHud();
    }

    public void BallLost(Ball lost)
    {
        lives--;
        ShowHud();
        lost.ResetToPaddle();
    }

    void ShowHud()
    {
        scoreText.text = "Score: " + score;
        livesText.text = "Lives: " + lives;
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Unity Game: Brick Breaker