Menu
CoddyTech

Score on Screen

Part of the Building Real Games section of Coddy's Unity journey. Lesson 30 of 56.

challenge icon

Challenge

Medium

Show how far the runner has come. Keep everything from the earlier lessons. The canvas has a DistanceText in the top left, and the scene puts it in a new distanceText field of GameManager. In GameManager:

  • Add using TMPro;, the field public TMP_Text distanceText; and a float named distance.
  • Every frame, add the distance the road moved: it moves speed meters per second, so speed * Time.deltaTime meters this frame.
  • Show it in whole meters, rounded down, as Distance: 44 m.

Press Play and watch the meters climb, then Check.

Try it yourself

using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public float speed = 10f;
    public float speedUp = 0.5f;
    public float maxSpeed = 20f;

    void Awake()
    {
        Instance = this;
    }

    void Update()
    {
        speed = Mathf.Min(speed + speedUp * Time.deltaTime, maxSpeed);
    }
}

All lessons in Building Real Games