Unity Development — Collectables

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.

--

--

--

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

Recommended from Medium

Mobileum Hiring Freshers For Software QA Engineer Position- Apply Now !

CSV resource for SQL and Data professionals

Strings- Python :

Classes… and other things

AWS Certification Learning Paths You Should Know About

https://www.vmexam.com/aws/saa-c01-aws-solutions-architect-associate

Sap Sd Case Study Pdf

How do you pick the right technology?

Data Manipulation Using Pandas

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
Christopher Graf

Christopher Graf

More from Medium

2D Mobile: IDamageable Interface

Day 103: Creating A Ledge Grab in Unity Part 1

How to get GIT Bash for Unity

Hidden Shortcuts to Make Level Design Easier in Unity.