Lesson 5.2 - Keeping Score

Tutorial

·

Beginner

·

+10XP

·

70 mins

·

(709)

Unity Technologies

Lesson 5.2 - Keeping Score

Overview:

Objects fly into the scene and the player can click to destroy them, but nothing happens. In this lesson, we will display a score in the user interface that tracks and displays the player’s points. We will give each target object a different point value, adding or subtracting points on click. Lastly, we will add cool explosions when each target is destroyed.

Project Outcome:

A “Score: “ section will display in the UI, starting at zero. When the player clicks a target, the score will update and particles will explode as the target is destroyed. Each “Good” target adds a different point value to the score, while the “Bad” target subtracts from the score.

Languages available:

Overview Video

1. Add Score text and position it on screen

In order to display the score on-screen, we need to add our very first UI element.



  1. In the Hierarchy window, right click or select + > UI > Text - TextMeshPro, then, if prompted, select the button to Import TMP Essentials.

  1. Rename the new object “Score Text”, then zoom out to see the canvas in Scene view.

  1. Change the Anchor Point so that it is anchored from the upper-left corner.

  1. In the Inspector window, change its Pos X and Pos Y so that it is in the upper-left corner.

2. Edit the Score Text’s properties

Now that the basic text is in the scene and positioned properly, we should edit its properties so that it looks nice and has the correct text.



  1. Change its text to “Score:”

  1. Choose a Font Asset, Style, Size, and Vertex color to look good with your background

3. Initialize score text and variable

We have a great place to display score in the UI, but nothing is displaying there! We need the UI to display a score variable, so the player can keep track of their points.



  1. At the top of GameManager.cs, add “using TMPro;

  1. Declare a new public TextMeshProUGUI scoreText, then assign that variable in the inspector

  1. Create a new private int score variable and initialize it in Start() as score = 0;

  1. Also in Start(), set scoreText.text = "Score: " + score;

Note: If your score text is split up onto two lines, change the Text Wrapping setting from Normal to No Wrap.



4. Create a new UpdateScore method

The score text displays the score variable perfectly, but it never gets updated. We need to write a new function that racks up points to display in the UI.



  1. Create a new private void UpdateScore method that requires one int scoreToAdd parameter

  1. Cut and paste scoreText.text = "Score: " + score; into the new method, then call UpdateScore(0) in Start()

  1. In UpdateScore(), increment the score by adding
    score += scoreToAdd;

  1. Call UpdateScore(5) in the spawnTarget() function

Note: If your score text is split up onto two lines, change the Text Wrapping setting from Normal to No Wrap.



5. Add score when targets are destroyed

Now that we have a method to update the score, we should call it in the target script whenever a target is destroyed.



  1. In GameManager.cs, make the UpdateScore method public

  1. In Target.cs, create a reference to private GameManager gameManager;

  1. Initialize GameManager in Start() using the Find() method

  1. When a target is destroyed, call UpdateScore(5);, then delete the method call from SpawnTarget()

Important: Make sure that the name of the Game Manager object in the Hierarchy matches the text in .Find("Game Manager") exactly.



6. Assign a point value to each target

The score gets updated when targets are clicked, but we want to give each of the targets a different value. The good objects should vary in point value, and the bad object should subtract points.



  1. In Target.cs, create a new public int pointValue variable

  1. In each of the Target prefab’s inspectors, set the Point Value to whatever they’re worth, including the bad target’s negative value

  1. Add the new variable to UpdateScore(pointValue);


7. Add a Particle explosion

The score is totally functional, but clicking targets is sort of… unsatisfying. To spice things up, let’s add some explosive particles whenever a target gets clicked!



  1. In Target.cs, add a new public ParticleSystem explosionParticle variable

  1. For each of your target prefabs, assign a particle prefab from Course Library > Particles to the Explosion Particle variable

  1. In the OnMouseDown() function, instantiate a new explosion prefab


8. Lesson Recap

New Functionality:


  • There is a UI element for score on the screen

  • The player’s score is tracked and displayed by the score text when hit a target

  • There are particle explosions when the player gets an object

New Concepts and Skills:


  • TextMeshPro

  • Canvas

  • Anchor Points

  • Import Libraries

  • Custom methods with parameters

  • Calling methods from other scripts

Next Lesson:


  • We’ll use some UI elements again - this time to tell the player the game is over and reset our game!

Complete this tutorial