Score System in the Unity Game Engine

Score System: What Is That?

In this tutorial, you will configure the game system to pick up the collectable item, also known as the score system. You will also add a user interface that displays how many scores you have picked up. The square game object already moves around the scene. It can collide with the coin object. Adding a user interface to the scene helps the player understand how the system picks up those objects and displays them. Unity includes a legacy system known as the Unity User Interface (UI). The UI (Canvas) is considered legacy in the Unity game engine. The most common older UI is the Unity Immediate UI, which runs in the editor. The latest widely available UI is UI Toolkit. It ships with the latest Unity Engine 6.

Score System and Recap

Hello, last week I wrote and posted about collectable items and the collision of an item with the square game object. This involved the player controlling the square game object with movement to make collisions with other objects. This process starts by creating a new game object in the hierarchy of the Unity game engine before proceeding further. After you create the game object, add a Circle Collider 2D component for the circle game object. Also, add another physics component to the circle game object, which is Rigidbody 2D. These components help create a functional game object in the game scene when played.

Score System: Restructure Coin Trigger Script

Previously, I added the coin trigger script, where the coin GameObject needs it to trigger itself. This script also helps the GameObject act as a trigger by enabling the component. Once that trigger runs, I chose to destroy the GameObject instead of doing something else. To update the current script, I changed those lines so they not only destroy the object but also add those values to the square GameObject. Usually, developers add this as score, especially in classic retro games. But users can also add achievements to the game, which requires many more assets. When you add score, integer points are enough, since that is actually the basic approach.

Adding a Variable Integer to the Script

Now, find the space to input the variable for the score system. The best way is to add a new script to the square game object by adding a component to the game object. Select the square game object, and in the Inspector, there is an option to add a C# script component. You can also use the Component menu in the top Unity toolbar to add a C# script to the square game object. These two methods are the basic ways to add a component to a game object. Once you add the C# script, rename it to SquareScoreSystem or another name that clearly defines its function as a score system.

Variable Score Inside MonoBehaviour class

Then, add an integer variable to the SquareScoreSystem script inside the MonoBehaviour class. Rename the integer variable to score so that it can be identified easily later. This score will be incremented during the game by the player controller when it collides with the coin game object. So every time the player collides with or picks up the coin, it will increase the score over time. That will make the square game object more valuable as the score system increases. Now, how do I actually connect the coin’s trigger game object with the player to increase those values? I will also add a new variable to the class known as the SquareScoreSystem singleton. This variable helps access the public variable of the square game object.

Subscribe to get access

Read more of this content when you subscribe today.

Initialize variable

In the Start function, this is one of the best methods to initialise your variables. Another function used for initialisation is Awake from the MonoBehaviour class. I chose the Start function since this tutorial is for beginners and new developers, where speed does not matter much when initialising this game project. Start by declaring your score variable as zero and setting the singleton to equal this in the line of code. The score value of zero is the starting value of the score variable. This reference is the current script of the SquareScoreSystem class, where you are accessing that script. By initialising these two variables in Start, you are preparing for the next task, which is accessing and incrementing the score when a collectable item object (coin) is collected.

Making a Function For the Score System

Next, build a function that increments the score system when other scripts call it. Name the function AddScore, and use it to add coins to the system when the player collects them. The AddScore function increments the score value every time the player character collects a coin game object. When the coin object triggers the event, another script calls this function through the singleton, and the public AddScore method in the class executes. This flow of adding coins is also known as the game mechanic for adding resources that create points for the player.

Java code defining a public void AddScore method that increments score by one

Calling Add Coin From Another Script

After adding the score function to the Square Control game object, the next step is to call that function from the CoinTrigger script game object. To do that, go back to the CoinTrigger script class, in the OnTriggerEnter2D function. Before destroying the coin game object, calling it at this moment or line of code is the best choice to make the system work. So you just need to call the script class SquareScoreSystem, then use the singleton variable. The singleton variable lets you access the public function AddScore, which should be called inside OnTriggerEnter2D.

Unity C# script adding one to the score when a player collision occurs

Adding UI For Display Score

Now, after adding the score increment, the next step is to add the user interface to the game scene to help display the information. To add the UI to the game scene, right-click in the Hierarchy section and find the UI (Canvas) option. Inside the sub-hierarchy of the UI (Canvas), find the Text, also known as TextMeshPro GUI. After you add the Text-TextMeshPro, it will automatically include a Canvas game object, and inside that is the Text-TextMeshPro. I then renamed that Text-TextMeshPro to the Score Text game object. This makes the game object easy to identify in the Hierarchy of the Unity game engine.

Conclusion of Score System

By following the tutorial above, you actually learn step by step how to access one script from another using a singleton. In general, you are already adding the score system to the game by using C# scripting. However, the singleton is generally C#, which is different from using the Unity system itself. Unity itself uses C# scripting with its own system, allowing it to work properly without the use of the Unity API. In my opinion, using the Unity API right now to access another script would require finding the GameObject, which needs a lot more coding, and I believe it is not suitable for beginners. Let me know what you think in the comment section. Goodbye.

I drafted this article from my own perspective and used AI to polish the language.
Concept art (created with generative AI, touched up manually).


Discover more from Duround Sanctum Studio

Subscribe to get the latest posts sent to your email.

One response

  1. […] Unity Game User Interface(UGUI): RECAP Last Post […]

Leave a Reply

The Simplest Method for Designing and Developing a Game