Menu
CoddyTech

Score on Screen

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

challenge icon

Challenge

Medium

Show how far the runner has come.

  1. In GameManager's Update(), after the speed line, add distance += speed * Time.deltaTime;.
  2. Then show it in whole meters: distanceText.text = "Distance: " + Mathf.FloorToInt(distance) + " m";.

Press Play: the meters climb faster as the road speeds up. Then Check.

Try it yourself

using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public float speed = 10f;
    public float speedUp = 0.5f;
    public float maxSpeed = 20f;
    public TMP_Text distanceText;
    private float distance;

    void Awake()
    {
        Instance = this;
    }

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

All lessons in Building Real Games

Practice on your own: Unity playground