Menu
CoddyTech

Make It Yours

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

The last feature is a best score that lasts between games. PlayerPrefs keeps small values on the player's device: PlayerPrefs.GetInt(key, 0) reads one, with 0 when nothing is saved yet, and PlayerPrefs.SetInt(key, value) saves one. Load the best when the game starts, and save it when a game ends with a higher score. Play Again reloads the scene, and the new GameManager loads the best again.

Then the game is yours to tune. Every number that sets how it plays is a field with a starting value in the code:

  • Ball: speed (9) and minVertical (0.3).
  • Paddle: speed (14), wideFactor (1.6) and wideSeconds (8).
  • GameManager: lives (3), dropChance (0.15), and the brick spacing.

The layouts are the GameManager's levels list in the Inspector: a digit is a brick with that many hits, a dot a gap, and / starts the next row, such as 1.3.1/22222. In your own Unity project, change values in the Inspector: once a script is on an object, the Inspector keeps its own copy, and a new starting value in the code no longer reaches it.

challenge icon

Challenge

Hard
  1. In GameManager, add public TMP_Text bestText (the scene links BestText) and an int best. In Start(), load best from PlayerPrefs under the key BestScore, 0 when nothing is saved. ShowHud() also shows Best: 0 in bestText, with the best.
  2. When the game ends with a score higher than best, store it in best, save it under BestScore and show it.

Then make the game yours: change the speeds, the lives, the drop chance and the power-ups' strength, and play until it feels right.

Press Play, set a best, then Check.

Try it yourself

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public Brick brickPrefab;
    public Transform wall;
    public string[] levels;
    public float spacingX = 1.75f;
    public float spacingY = 0.75f;
    public float topY = 6f;

    public Ball ball;
    public int lives = 3;
    public TMP_Text scoreText;
    public TMP_Text livesText;
    public GameObject gameOverPanel;
    public GameObject[] powerUps;
    public float dropChance = 0.15f;

    int score;
    int level;
    int bricksLeft;
    List<Ball> balls = new List<Ball>();

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        balls.Add(ball);
        BuildLevel(0);
        ShowHud();
    }

    void BuildLevel(int index)
    {
        level = index;
        string[] rows = levels[index].Split('/');
        for (int row = 0; row < rows.Length; row++)
        {
            string line = rows[row];
            float left = -(line.Length - 1) * spacingX / 2f;
            for (int col = 0; col < line.Length; col++)
            {
                char c = line[col];
                if (c == '.') continue;
                Vector3 pos = new Vector3(left + col * spacingX, topY - row * spacingY, 0f);
                Brick brick = Instantiate(brickPrefab, pos, Quaternion.identity, wall);
                brick.SetHits(c - '0');
                bricksLeft++;
            }
        }
    }

    public void BrickBroken(Brick brick)
    {
        score += brick.points;
        bricksLeft--;
        ShowHud();
        if (Random.value < dropChance)
        {
            GameObject prefab = powerUps[Random.Range(0, powerUps.Length)];
            Instantiate(prefab, brick.transform.position, Quaternion.identity);
        }
        if (bricksLeft == 0)
        {
            balls[0].ResetToPaddle();
            BuildLevel((level + 1) % levels.Length);
        }
    }

    public void BallLost(Ball lost)
    {
        if (balls.Count > 1)
        {
            balls.Remove(lost);
            Destroy(lost.gameObject);
            return;
        }
        lives--;
        ShowHud();
        if (lives == 0)
        {
            gameOverPanel.SetActive(true);
            lost.gameObject.SetActive(false);
        }
        else lost.ResetToPaddle();
    }

    public void SplitBall()
    {
        Ball source = balls[0];
        Ball left = Instantiate(source, source.transform.position, Quaternion.identity);
        left.Launch(new Vector3(-0.6f, 1f, 0f));
        balls.Add(left);
        Ball right = Instantiate(source, source.transform.position, Quaternion.identity);
        right.Launch(new Vector3(0.6f, 1f, 0f));
        balls.Add(right);
    }

    public void PlayAgain()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

    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