Adding Collectables to our 2.5D Platformer Prototype in Unity 2021

GameDev Dustin
5 min readJul 13, 2022

--

In the first article for this project series, we haphazardly added some spheres to represent collectable objects in our game.

We’ll add some more functionality to these in this article.

But first, let’s look at what we’ve already got.

We have a prefab with a material assigned to it, the collider set to “Is Trigger”, and tagged as “Collectable”.

We’ve also placed three Collectable game objects in our scene from this prefab.

Planned Functionality of Collectables
We’ll implement our collectable game objects with functionality of coin collecting.

Every collectable represents 1 or more coins which the player will collect simply by colliding with any collectable game object.

We’ll also display the number of coins collected as part of a User Interface (UI).

To create this functionality, we’ll need to create a UI Manager script and a Coin script.

We’ll also need to add a UI Canvas and some text / graphic elements for the UI display.

Create the Canvas and Text Elements
Creating the Canvas is straight-forward and as shown above.

We’ll also add two text fields which will prompt us to import Text Mesh Pro Essentials.
Go ahead and import that, then rename the text element as “txtCoinsTitle”.

Create another text element and name it “txtCoinsCount”.

As shown above, reposition and enter default values for the Text Input fields.
Align as needed.

Let’s also go ahead and set the UI Scale Mode to “Scale With Screen Size” and a default 1920 x 1080 (HD) resolution on the Canvas element.

Renaming the Collectable Prefab
Unfortunately, renaming a prefab breaks the prefab connections to existing game objects.

After renaming the Collectable prefab to “Coin”, duplicate and place next to existing but broken Collectable game objects.

Copy/Paste the transform component from the existing game objects to the new game objects.
Remove the old game objects afterwards.

Add UIManager & Coin Scripts
Next, we need to create a script for our UI and our Coin.

I’d suggest naming them “UIManager” and “Coin”.

Due to existing scripts with those names, I had to use the alternative names shown above.

The UIManager Class / Script
The UIManager script is pretty straight forward.

We just want to be able to update the txtCoinsCount text field of our UI.

We’ll then assign our txtCoinsCount element to the UIManager script component in the appropriate field.

The Coin Class / Script
Let’s setup our Coin script to generate a random number of coins contained in the collectable.

We’ll add a public method to return the number of coins when the player collides with the collectable.

If we hit play in the Unity Editor, we can see that each Coin game object generates a random number of coins.

The PlayerInventory Class / Script
Instead of cluttering up our PlayerController class/script, we’ll create a new script for tracking the player’s inventory.

In this script we’ll check for collisions with collectables, add them to the player inventory, and update the UI.

Our PlayerInventory script just needs to keep track of the number of coins collected and a reference to the UI Manager.

Whenever the player character’s collider triggers, we’ll check the tag of the object collided with and if it is both tagged as “Collectable” and the name starts with “Coin”, we’ll add the number of coins it represents.

Before we can test this, we’ll need to assign the UIManager to the PlayerInventory script component.

Now if we hit play, we can see our coin count updating as expected.

Making the Collectables Disappear After Player Contact
We don’t want the player to collect the same collectables repeatedly so we need to make these collectables vanish after contact with the player.

In the Coin class / script, add the public HideCollectable() method as shown above.

In the PlayerInventory class / script, I’ve assigned the Coin script to avoid using GetComponent<>() a second time.

We also call the HideCollectable() public method from here.

And there you go, it works!

--

--

No responses yet