Menu
CoddyTech

Make It Yours

Lesson 14 of 14 in Coddy's Unity Game: Snake 3D course.

One feature is left: a best score that survives closing the game. PlayerPrefs keeps it: read it with a default in Start(), and save it at game over when the score beats it. A racing game keeps its best lap the other way round, since a lower time is better:

if (lapTime < bestLap)
{
    bestLap = lapTime;
    PlayerPrefs.SetFloat("BestLap", bestLap);
}

Then the game is yours to tune. Snake's numbers set its feel: tick 0.25 makes a calm start and 0.1 a fast one, speedUp 0.01 speeds the snake up sooner, and minTick 0.05 makes a frantic end game. This player's Inspector is read only, and the course's scene leaves these three fields at the values in your code, so change them where they are declared and press Play. In your own Unity project, change them on the Snake component in the Inspector: a new default in the code does not reach a component already in a scene.

challenge icon

Challenge

Hard

Keep a best score between games, then tune the game.

  1. In GameManager, add public TMP_Text bestText; and private int best;
  2. In Start(), best = PlayerPrefs.GetInt("BestScore", 0); and show "Best: " + best.
  3. In GameOver(), when score beats best, set best, save it under BestScore and show it.
  4. Tune tick, speedUp and minTick where they are declared in Snake.

Press Play, set a best and play again: Best stays top right. Then Check.

Try it yourself

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

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public Snake snake;
    public GameObject foodPrefab;
    public TMP_Text scoreText;
    public GameObject gameOverPanel;
    public Button playAgainButton;
    private Transform food;
    private int score;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        scoreText.text = "Score: 0";
        playAgainButton.onClick.AddListener(PlayAgain);
        food = Instantiate(foodPrefab).transform;
        MoveFood();
    }

    public bool IsFood(Vector3 cell)
    {
        return cell == food.position;
    }

    public void Eat()
    {
        score++;
        scoreText.text = "Score: " + score;
        MoveFood();
    }

    public void GameOver()
    {
        gameOverPanel.SetActive(true);
    }

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

    void MoveFood()
    {
        List<Vector3> free = new List<Vector3>();
        for (int x = -snake.edge; x <= snake.edge; x++)
        {
            for (int z = -snake.edge; z <= snake.edge; z++)
            {
                Vector3 cell = new Vector3(x, 0f, z);
                if (!snake.Occupies(cell))
                {
                    free.Add(cell);
                }
            }
        }
        food.position = free[Random.Range(0, free.Count)];
    }
}
quiz iconTest yourself

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

All lessons in Unity Game: Snake 3D

Practice on your own: Unity playground