Prototype: Collectables


Our game wouldn’t be complete without some way to quickly add collectables to the game. For our prototype, we don’t have strict requirements for what these collectables will do yet, but we can implement the groundwork so that as soon as ideas arise, we can quickly implement them to test.

The Challenge

Our challenge is to create a simple collectable system where the player can cause a trigger collision and identify what type of collectable caused the trigger. For now, we’ll simply log the type of collectable to the console, since we don’t know exactly what each type will do.

Collectable Prefab

Let’s create the prefab first. I’ll create a new Sphere and create a new Material to add some color. I’ll also create a new tag called Collectable and assign it to the GameObject, then make sure the collider is set to be a trigger, finally drag and drop it to the Prefabs folder to create a new prefab.

Sphere GameObject

Next, I’ll create a new script called "Collectable" and add it to the scripts folder. Inside the script, I’ll add movement just like we did in the enemy script. I’ll add a variable speed and destroy the collectable when it goes off-screen.

Collectable Code

Collectable Type

On to the more interesting bit, let’s create a way to make each collectable distinguishable. Again, I’m going to do this in a way that’s fast for us to prototype ideas. Once we know what will make it to the final game, we can create a more elegant system.

For now, I’m going to identify collectables by assigning an enum value. I’ll create a separate file in the Scripts folder named CollectableEnum and add some placeholder types we can change later.

CollectableType Code

Back in our Collectable script, I’ll add a private field for CollectableType named type with the SerializeField attribute. Switching back to Unity, we can see that now we can easily select what type of collectable we want an instance to represent.

Unity Enum Selection

We will need to access this value from the Player script, so let’s make a get accessor.

Get Accessor

To make it easy to apply collectables to the player, I’ll add a Rigidbody component (no gravity), then inside the Player script add an OnTriggerEnter method. Now, when the Player collides with a Collectable, we will log the type of Collectable to the console.

Here’s the updated Player script: Player Script

Log Collectable GIF


That’s it! Now we have a way to quickly set up new types of collectables during the prototype phase. This can certainly be improved later, but it will serve us well for quick iteration during playtesting.

Stay tuned for the next post.
Take care.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s