Score on Screen
Part of the Building Real Games section of Coddy's Unity journey. Lesson 30 of 56.
Challenge
MediumShow 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 fieldpublic TMP_Text distanceText;and afloatnameddistance. - Every frame, add the distance the road moved: it moves
speedmeters per second, sospeed * Time.deltaTimemeters 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
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap