Creating Collectables in Unity

Preparing the coin object

We have earlier created spheres as placeholders for our coin. Make sure they have no gravity, but the isTrigger is selected. Override to apply it to all the coin instances.

  • UI Manager Script which updates our UI

Collectable Script

In the collectable script we are going to detect whether our player has collected the coin. In terms of unity we check for an OnTriggerEnter. As we are in 2.5D we use the 3D collider. So our pseudocode looks something like this:

//if the player triggers the coin
//add the coin to the player coin score
//update the UI Manager text
//destroy the coin object
Collectable OnTriggerEnter

Adding a Canvas and Text

To display the coin score we add a UI Canvas and simple text in Unity

Adding a canvas and text.
Text UI Element to hold the coin score

UI Manager Script

We add a UI Manager script and either add it to a UI Manager or here I will simply attach it to the canvas object. Remember that whenever we want to access the UI in a script we need to add the UI library in the script:

using UnityEngine.UI;

--

--

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
Andrea Zilio

Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.