High Score
Part of the Building Real Games section of Coddy's Unity journey. Lesson 24 of 56.
A high score is the best result of every game played, so it lives in PlayerPrefs, and it changes only when a game beats it. This pinball table checks when the last ball drains:
void GameOver()
{
int best = PlayerPrefs.GetInt("PinballBest", 0);
if (score > best)
{
PlayerPrefs.SetInt("PinballBest", score);
PlayerPrefs.Save();
Debug.Log("New high score: " + score);
}
}The comparison is what makes it a high score. Without it, a game of 300 after a best of 5000 would save 300, and the record would be gone. Compare with the saved best, not with something that starts at 0 in every game: a field that nothing loaded is 0 after every restart, so any score beats it. score > best keeps the old record on a tie, while score >= best would call a tie a new high score.
To show the high score during play, load it into a field in Start() and show that field. When a game beats it, update the field, PlayerPrefs and the text together, so the screen and the saved value never disagree.
Challenge
MediumBalloon Pump: hold Space to pump air into the balloon, 20 points a second, and let go to cash in the points. Pump past 100 and it pops, for 0 points. The pumping, the pop and the points are written, and when the round ends EndRound() is called once, with the round's score in points. Add a high score, saved under the key BestPoints:
- In
Start(), load the best intobest(0 for a new player), soShowBest()shows it. - In
EndRound(), whenpointsbeats the best, save it, show it, and shownewBestText. A round that does not beat the best changes nothing.
Press Play and pump a few balloons (Stop and Play starts a new round), then Check.
Try it yourself
using UnityEngine;
using TMPro;
public class BalloonPump : MonoBehaviour
{
public TMP_Text pointsText;
public TMP_Text bestText;
public GameObject newBestText;
public float pumpRate = 20f;
private float air;
private int points;
private int best;
private bool roundOver;
void Start()
{
ShowBest();
}
void Update()
{
if (roundOver) return;
if (Input.GetKey(KeyCode.Space))
{
air += pumpRate * Time.deltaTime;
transform.localScale = Vector3.one * (1f + air / 50f);
pointsText.text = "Points: " + Mathf.FloorToInt(air);
if (air > 100f)
{
points = 0;
pointsText.text = "Pop! Points: 0";
GetComponent<Renderer>().enabled = false;
roundOver = true;
EndRound();
}
}
else if (Input.GetKeyUp(KeyCode.Space))
{
points = Mathf.FloorToInt(air);
pointsText.text = "Points: " + points;
roundOver = true;
EndRound();
}
}
void EndRound()
{
// Save a new best here
}
void ShowBest()
{
bestText.text = "Best: " + best;
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
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