Creating Collectable Objects In Unity And Updating The UI Once Collected

In most games, you will find a number of things that you can acquire as a player such as new weapons, healing items, powerups, or secret collectables.

This is a feature that is added for a number of reasons. Some developers use pickups as a way to stimulate exploration while others use them to break up the monotony of running between areas. Some games have even used collectable objects in order to tempt players into performing complex or risky maneuvres in order to get them.

No matter how you choose to implement them, collectables can be a fun and engaging addition to your game.

So let’s see how to create them!

First off, make sure your Player object is set with a Tag. I’ll tag my player as “Player”.

Then right-click in your hierarchy and create a new “UI Text object”.

Next, add an object to your scene which will act as your collectable object.

Create a new C# script and name it UIManager. Make sure to add the “Namespace” “using UnityEngine.UI;”.

Within this UIManager script, you’ll need to create a serialized Text object and a new method to update your UI.

Inside of your “Player” script make a reference to the “UIManager” script. Then add a new “Private Integer Variable”. And then create a new method with your integer variable being increased as well as a call to the method you just added to your UIManager script.

Now create another new C# script to add to your collectable game object, I’ll name mine “Coin”. And add the code for collision detection as well as a call to your new method on your “Player” script.

Lastly, you’ll need to drag your text object in your hierarchy into the reference slot for it on the “UIManager” script.

And that’s it!

--

--

--

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

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

Recommended from Medium

Code inspection with SonarQube and Docker

SonarQube dashboard

Creating Custom Server in .Net Core

Promise only refers to a type error: how to solve it?

All About Web-Frameworks

How to store subscriptions? A practical guide and analysis of 3 selected databases

Keep your Python code efficient

Accelerate IT Service Delivery With The Improved UI For AssetSonar’s Jira Integration

Get Faster Navigation And Powerful Control With The Improved UI For AssetSonar’s Jira Integration

How To Use Abstract Classes, Abstract Methods, and Virtual Methods In C#

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
Adam Reed

Adam Reed

Hi, my name is Adam Reed and I am a software engineer specializing in Unity and C# development. Feel free to scroll through and check out some of my work!

More from Medium

Making nice cameras in Unity with the Cinemachine plugin

Variables! — The building blocks of programming

Variables, because fixed is a worse option

Day 102: Character Animation Part 5