Unity Development — Collectables

Christopher Graf
3 min readJul 27, 2021

We have talked about triggers before and how they can be used for numerous things. Today we are going to combine the use of triggers with some other techniques we have learned to create collectables.

Make a Sphere object or some other object for your Collectable. Make sure its collider is set to be a trigger, add a Rigidbody, and set its tag is “Collectable” or some name representing what it is. Create a Prefabs folder in the Project Window if you don’t already have one. Drag over your Collectable object to make is a prefab. Place more prefabs of the Collectable all around your scene.

Have a Player object with a script that you can move. They should also have a collider of some sort. This does not need to be a trigger. If they have a Character Controller, that component provides a collider.

In the player script add a variable to represent the number of collectables the player has grabbed. Create an OnTriggerEnter() method and check for the tag that you gave your collectables. If they match, then it should destroy the collectable and add 1 to your variable.

To take this a step further, you could create a UI Text that displays your collectable count. Place it wherever you like on the screen with the proper Canvas settings. Make a UIManager script and place it on the Canvas.

Make this UIManager a Singleton and then we will be able to call a public method on it from anywhere. You could use this to update your collection display number. Try something like this.

In the Player’s OnTriggerEnter, call to this Instance and update the visual display.

There you have it. Now you have collectables and when they are collected, you have a clear sign of it.

--

--