2D Mobile Game: Gem Count UI

Ryan McCoach
3 min readMay 15, 2022

Intro

In this article, we will cover how to update the Gem Count text in the UI.

UI Manager

The UI Manager is the script that is in charge anything and everything that has to do with the User Interface. In order to update the gem count, we need access the gem count text UI element. Declaring a Text type variable with serialized field attribute gives us the ability to assign the Gem Count Text element in the inspector.

Simply drag the Gem Count Text object to the UI Manager script. Now we can access this element through the UI Manager script.

Every time the Player collects a gem, we will need to say to the UI Manager to update the gem amount by the value of the gem that was collected.

This means a public method will be needed, so the Player can call every time it collides with a gem. Also, an int parameter will be added to the method given us the flexible to tell the UI Manager how much we want to change the gem count by when the Player calls the method.

Simply, when the method is called, we access the gem count text and set it to an empty string plus the gem amount that was just collected.

Gem, Player, UI Manager Communication

The Gem game objects can detect when the Player collides with it and when it does we grab and access the Player script before destroy the Gem game object. Before destroying the gem, we want to tell the Player to add gem value to the gem total. Each enemy drops a different value gem.

In the Player, we have a public int variable (gems) keeping track of the amount of gems collect. We create the public method (AddGems) that is being called from the Gem script that pass in the gem amount and adds the gem amount to the gems total.

Once the new gem amount has been added, we need to notify the UI Manager by calling the Update Gem Count method and pass in the Player’s current gem total (gems).

A Subtract Gems method is part of the Player’s script for when the Player visits the shop and buys an item.

When the Shop script has the cost of each item and when the Item is bought we call the Subtract Gems method from the Player and pass in the cost of the item.

--

--