Scoring - Part 1 (Add scoring to a 2D game)

2D Game Design with Unity

🕑 This lesson will take about 14 minutes

In this lesson, you will learn how to write code to implement scoring in a 2D platformer game in Unity. The video below demonstrates how to increase the score when the player touches a collectable object (eg. a gem or coin). In the next lesson, we will look at how to add a user interface (UI) to display the score on-screen during the game.

The code to implement scoring will be written in two separate scripts:

  • ‘PlayerController.cs’ - part of the code (for detecting collision with collectables and increasing the player’s score) is added to this existing script that we have worked on throughout the course and which is attached to the Player GameObject

  • ‘Scoring.cs’ - this is a new script and will be created to store the player’s score which can be accessed and modified across different scenes/levels in the game.

Watch the video below and then scroll down for the sample code for each script.

Game art, tiles and sprites from the Free Pixel Space Platform pack are used in this lesson video and can be downloaded from the Unity Asset Store.

Is YouTube blocked at school? Watch on Google Drive.

Sample code for PlayerController.cs script

Sample code for Scoring.cs script