Collectables for the Player

How to create a Collectable System in Unity

Tristan Engel
2 min readJan 2, 2022

In this article we will make the placeholder collectables actually collectable.

Setup Steps

We created 3 scripts. For the player, for the collectables and a UI Manager.

Create a UI Text game object
Attach the UI Manager script to the canvas
Attach the Collectable script to all collectables (use prefabs)
Make sure the collectables have a rigidbody and isTrigger is checked on the collider

Pseudocode — Collectable System Scripts

We will use all 3 scripts. The player script will hold the amount of collectables collected. The collectable script will check for collisions and if it is hit by the player it will tell the player script to increase the score. The player script will then tell the UI Manger to update the displayed number.

Collectable Script

private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
other.GetComponent<Player>().Collectable();
Destroy(this.gameObject);
}
}

Player Script

Handle to our UI Manager script assigned through the Inspector.

[SerializeField] private UIManager _uIManager;

Variable declared at the top to hold our score.

private int _collectables;

Our public method that is called from our Collectable script.

public void Collectable()
{
_collectables++;
_uIManager.UpdateCollectableText(_collectables);
}

UI Manager Script

Don’t forget at the top, because we need to use the UI library of the Unity engine.

using UnityEngine.UI;[SerializeField] private Text _collectableText;

public void UpdateCollectableText(int collectableCount)
{
_collectableText.text = "Balls: " + collectableCount;
}

--

--

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.