Best Time
Part of the Building Real Games section of Coddy's Unity journey. Lesson 56 of 60.
The last step of the platformer: the player now runs, jumps, rides, respawns, collects gems and crosses into the next level, and each level keeps its best time in PlayerPrefs.
Challenge
MediumEach level remembers its best time.
- In
FinishLine, addusing TMPro;,[SerializeField] private TMP_Text bestText;and a run time growing byTime.deltaTime. - In
Start(), show the best saved underBestTime_plus the level's name asBest: 12.4 s, orBest: none. - At the finish, before loading, save a first or faster run time.
Press Play, set a time and beat it. Then Check.
Try it yourself
using UnityEngine;
using UnityEngine.SceneManagement;
public class FinishLine : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Player>() == null) return;
Scene level = SceneManager.GetActiveScene();
Debug.Log(level.name + " complete!");
int next = level.buildIndex + 1;
if (next >= SceneManager.sceneCountInBuildSettings)
{
next = 0;
}
SceneManager.LoadScene(next);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Building Real Games
10Project: 3D Platformer
The CourseMoving Platforms2Buttons 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 SwapPractice on your own: Unity playground