Skip to main content
‹ Back to Tutorials
Intermediate Unity

Learn how to make Games: Unity (Part 9) - Adding Score to a UI with Unity 3D and Playmaker!

Nov 1, 2020
About this tutorial

Hi I'm Jerry from Bliz Studio LLC. In this tutorial, we are going to be adding score to a variable every time we kill an enemy. We will also be using that score and creating a UI with the score that we are gathering using Unity and Playmaker. How to Make a Video Game With Unity3D and Playmaker Ser

Written Guide

Part nine of this learn-to-make-games series puts a live score on screen. The approach is to count destroyed enemies into a global variable, then display that number through a TextMeshPro UI element. Along the way it covers a little scene cleanup and the int-to-string conversion that trips up a lot of beginners.

Tidy the scene first

Before adding UI, remove the leftover test enemy (enemies now spawn at runtime), rename the two background quads to background one and background two, and tuck them plus the off-screen destroyer into an empty parent named background. The destroyer's ship sprite can be turned off since it only needs its collider. A clean hierarchy makes the new score system easier to wire up.

Create the score UI

Right-click in the hierarchy and add a UI > TextMeshPro object — Unity automatically creates the Canvas container it needs, and you'll be prompted to import TMP Essentials. Rename the canvas UI-score and position the text in the upper-left corner. A useful habit: type XXX as placeholder so you can judge how much space the digits will take before any logic exists.

Convert the number into displayable text

Add an FSM to the score UI with a Listener state. Scores are whole numbers, so create a global int variable named score, then use Convert Int To String to turn it into a local string called score count. This conversion is required because a TextMeshPro field displays a string, not an integer — you can't push a raw number straight into the text.

Push the value into the text field

Drag the score text object into the FSM and add Set Property targeting the TextMeshPro component. From its long property list, choose text, then its string field, and set the value to score count with Use Variable checked, running every frame. Now the on-screen number mirrors the global score — change score to 5 in the editor and the display updates to match.

Add to the score when an enemy dies

In the enemy prefab's destruction FSM (which already plays audio, shakes the camera, fires particles, and hides the sprite), insert an Update Score state between blow-up and destroy-self. Use Int Add on the global score with a value of 1 — change that to 5 or 10 for a different scoring scheme — and don't run it every frame since it should fire once per kill. To remove a noticeable delay, reorder the states so the score updates first, then the blow-up effects play, then the object destroys. Finally, drop an Image into the canvas and set its sprite source to the enemy art for a small badge beside the number.

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 →