Unity Guide
Creating collectables | Unity
A quick guide about how to create collectables with Unity

Objective: Create collectables and implement a system to collect them with Unity.
In the last post I covered how to implement a double jump with Unity. Now, it’s time to implement some collectables and a way to display the total for our platformer game.
The collectables
In order to represent the collectables we’re going to use primitive assets. As you can see in the next image, we have 3 spheres above the platforms to represent a collectable item:

Implementing the collection mechanic
Then, in order to implement the collection mechanic, let’s create 2 new scripts:
- Collectable
To handle the collectable mechanic.
- UIManager
To handle the user interfaces where the total number of collected items will be displayed.


Now, let’s make sure that every sphere:
- Has the Is Trigger property enabled in the collider component
- Contains a rigidbody (that doesn’t use gravity)
- Gets attached the respective script


Then, let’s create a new Text element to display how many collectables (Coins, power-ups, etc.) does the player has collected:


Also, let’s attach the UIManager script to the canvas so we can handle the user interfaces with the script:



Next, let’s open the collectable script and add the next namespace so that we’re able to handle the collection mechanic using C# Action Delegates:

Then, let’s create a public static Action Delegate to take the respective actions in other scripts when the coins are collected:

Now, in order to implement the collection mechanic we’ll use the OnTriggerEnter method and check if the other collider belongs to the player. If that’s the case, let’s call the Action Delegate (if it isn’t null for best practices) and then destroy the collectable object:

Note: The player must be tagged with the Player tag to be identified when a collision with the collectable happens.
Once the Action Delegate is created within the collectable class let’s open the Player script and add:
- A private variable to store the number of coins
- A public property to return only the value of the private variable


Then, let’s create a new method that will add a coin every time it gets called:

Now, let’s add the method to the static Action Delegate from the Collectable class to be able to call it every time that the coins get collected:

Finally, as good practice, let’s remove the method from the Action Delegate when the player gets destroyed using the OnDestroy method:

Displaying the total
And now, to display the total of coins that the player has collected, let’s open the UIManager class and add the UnityEngine.UI namespace to be able to handle the UI elements from code:

Then, let’s create variables to:
- Store a reference to the UI Text element
- Store a reference to the Player script

Note: It’s probable that I will store the number of coins within a game manager and not in the player itself. I’m going to store a reference to the Player script for now. Don’t forget to use [SerializeField] to set the references through the inspector.
Next, let’s grab the references to the UI Manager component through the inspector:


Now, let’s create a new method to update the Text element that displays the total of coins. This will modify the text by calling the property of the Player script:

Finally, let’s add the respective method to the Action Delegate from the Collectable class so that the Text element gets updated every time that a coins get collected. Also, don’t forget to remove the method using the OnDestroy method:


If we run the game with Unity, we’ll be able to collect the spheres and the UI element will display the total collected:

And that’s it, we created collectable items with Unity! :D. I’ll see you in the next post, where I’ll be showing how to move platforms for our platformer game with Unity.