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!