Platformer: Collecting Coins

Introduction

In this article, we will cover setting up the player be able to collect coins and display the amount of coins that have been collected.

Goals

  1. When the player collides with the coin it will be destroyed
  2. When the coin is collected it will update the UI with the current coin count.

The UI Manager

We are going to add a UI element of Text Mesh Pro to the scene. This will create a Canvas and Event System. A script will be created, UIManager, and attached it to the Canvas. In the script, we will need to access the Text Mesh Pro library by declaring “using TMPro”. Then, we are going to declare a Text Mesh Pro Text and serialized.

We will assigning the Text to the UI Manager script in the Inspector.

Back in the UI Manager script, we will create a Method that will allow the updating of the Text on the UI. A int parameter is needed when we use the Method in the Player script and will be added to the coin count text.

The Player

In the Player script, we will create a int variable that will hold the coin count and a UIManager variable that will hold a reference to the UI Manager. On Start, we are going to get a handle of the UI component and null check it. Finally, we will set the coin count to 0 and call the Update Coin Display method from the UI Manager and pass in the coin count.

We get this when the game starts.

The last thing we need to create an AddCoin method that will increment the current coin count by 1 and call the Update Coin Display from the UI Manager and pass in the coin count. This method will be called in the Coin script.

The Coin(s)

In the Coin script, which is attached to the Coin prefab, we will use a OnTriggerEnter event. Just make sure there is a Rigidbody attached to the coin, uncheck Use Gravity and in the collider we need to check Is Trigger. We will check the tag of what the coin collided with and see if it is the Player. If it is, create a local variable of Player and store the Player component. Once we have access to the Player script and we preformed a null check we will call the Add Coin method from the Player. Lastly, we destroy the coin GameObject so it makes it look like it was “collected”.

Some script communication to get this to work, but that is how to create a coin collectable with an updating UI. Cheers everyone!

--

--

--

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

Recommended from Medium

Platformer: Player Animation Part 3 (continued)

How to use Mongo shell in windows system

Remove silence from your audio in Python with pydub

Writing a basic REST API with Flask and Flask Restful (A flask extension) in Python

Common Pitfalls of AWS Lambda with Python

Why does LINEARIZE give different results if my Simulink Solver

Design separation in data-driven tests

Web scraping

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
Ryan McCoach

Ryan McCoach

More from Medium

Power-Ups

Cinematography in Unity — Fade to Black

Too Many Lasers!

Platformer: Player Animation V — Idle Jump