Day 87: Creating Collectables in Unity

Objective: Create collectables and update the UI and variable on the player.

In our first article for this game, we created objects to become our collectables. Today we will turn them into real collectables!

The first thing we will complete will be the UI. In the Hierarchy, create a Text -TextMeshPro.

When you add a TextMeshPro for the first time to a project, it’ll ask you to import TMP Essentials, import them so we can use TextMeshPro. After the essentials are imported, it’ll allow you to import examples and extras. If you want some more fonts import those as well.

We will align it to the top left corner and in the Text input component, add in Coins: 0.

Now to the logic. Will we need to create a script called Coins, since the collectables are coins. Inside we will check for OnTriggerEnter. We will check to see if it was the player that entered. We will also need to grab a reference to the Player script.

Before continuing on the coin, we need to create a function in the player script to add coins as well as create a UIManager to update the UI text. Let’s move to the UIManager first.

For our UIManager, we will be using the singleton pattern to implement it. We used the MonoSingleton to the singleton for the UIManager. Refer to my Game Pattern article.

Now in our UIManager script we update the MonoBehaviour to MonoSingleton<UIManager>. Once that is done, we need to get a reference to the Coin_Text element. Since we are using TextMeshPro, we need the namespace TMPro.

Next will be to get a reference to it.

Now a function that will update the text to the correct amount that the player has. This function will take in a coin amount to display. Using TextMeshPro is different from the regular text. We need to use SetText to change the Text on the UI, as seen below.

Our UIManager for Coins is now done. Time to head over to the player script.

In our player we need a variable to hold the amount of coins.

We will need to create a function to add a coin each time the coin is collected. In this function we will just increment by one, for now, and update the UIManager by calling the instance and the function and passing in the coins.

Now back to the Coin script. All that is left is to update the coins on the player and destroy the object to give the illusion it was collected.

And that is it. We can now collect the coins and both the Player and UI gets updated with the correct amount.

Thank you for reading and have a wonderful day!!

--

--

--

I am passonate on learning to program and use Unity to become a skillful Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Guide to a Bastion Less World. Let’s SSH and RDP via AWS Session Manager!

Invoke AWS Lambda from AWS Step Functions with Terraform

Encapsulation — Core Voilà

Keeping it simple

From Front End to Full Stack

Design Patterns — Essential for Software Development

Every Day Calendar HTML5 edition

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tyler Smallwood

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer

More from Medium

Loading Scenes In Unity

Unity Interface Layout

Pushing Objects in Unity to Complete Puzzles

Creating An Advanced 2D Player Controller Without Any Unity Components! (Part 1)