Skip to main content
‹ Back to Tutorials
Intermediate Unity

Unity 3D Playmaker Creating Flappy Birds - Adding High Score

Jan 16, 2022
About this tutorial

In this mini Unity 3D Playmaker Creating Flappy Birds tutorial, we will be taking a look at adding high score to the game. In this fourth installation of the tutorial, we will using Playmaker to get player prefs to check for high score, and then during gameplay, to set the high score. Unity packag

Written Guide

This entry in the Flappy Bird series adds a persistent high score — one that survives closing the game — using Unity's PlayerPrefs through Playmaker. The logic lives in the existing detect pipes FSM, since that's already where scoring and game over are handled: load the saved score at startup, compare on death, save and display when it's beaten.

Load the saved score at startup

First add two text objects to the game over UI: a "high score" label and the number itself. Then, in the detect pipes FSM, add a Get High Score state and make it the start state, ahead of listen for pipes, with a finished transition into it. Inside, use PlayerPrefs Get Int. PlayerPrefs stores keyed values on the device — high scores, remaining lives, whatever — so set the key count to 1 and create a key named high score. Store what comes back in a new int variable, hi score num. If nothing was ever saved, you simply get zero.

Check the score on game over

The FSM already adds to pipe score for each pipe cleared and heads to game over on a hit. Insert a score check state between the hit detection and game over. Use Int Compare: pipe score against hi score num. Equal or less than routes straight to game over — nothing to update. Greater than sends a new add to high score event into an update high score state. Rewire the pipe-hit transition so it lands on score check first.

Save it twice

Beating the record means updating two places: the device and the running game. In update high score, use PlayerPrefs Set Int to write pipe score into the high score key — that's the permanent save. Then use Set Int Value to copy pipe score into hi score num, so the in-game comparison stays correct if the player immediately plays again. Finish with a transition to game over.

Put it on screen

UI text needs a string, not an int, so in the game over state add Convert Int To String, turning hi score num into a new high score text variable. Drag the high score number text object into the state, choose Set Property, scroll the property list to text, and feed it high score text. That's the full loop: load from PlayerPrefs, play, compare on death, save to both device and game, convert, and display. Test it — clear two pipes, crash, and the game over screen shows a high score of 2, saved for next session. As Jerry admits on camera, being terrible at Flappy Bird makes this feature easy to test.

Unity tools by Bliz Studio
Built for our own games — now on the Asset Store

Multi Tag — unlimited hierarchical tags for any GameObject. Hierarchy Pro — style your whole hierarchy with rules. Made by a Unity dev, for Unity devs.

Explore our Unity assets →