Unity Guide

Creating a loot system | Unity

A quick guide about how to create a simple loot system in Unity

Fernando Alcantara Santana
Nerd For Tech
Published in
4 min readNov 8, 2021

--

Objective: Create a loot system for the player to collect diamonds in a 2D game with Unity.

In the last post I covered how to implement a hitbox detection system with Unity. Now, it’s time to create a loot system in order for our player to be able to collect diamonds in our 2D game.

Current state

To start, let’s take a look at the current state of our 2D game. Currently, we can attack and receive attacks from the enemies:

Also, we already have several diamonds across the environment that we’ll be able to collect with the loot system:

Creating the diamonds

In order to introduce the respective diamonds we sliced a sprite sheet that contains the diamond sprite with an animation using the 2D sprite editor:

Then, we created a new gameobject with the next components:

  • Sprite Renderer
  • Animator
  • Rigidbody 2D

The gravity scale is set to 0 so that the diamond doesn’t fall and remains on its position.

  • Circle Collider 2D

The Is Trigger property is enabled to check when the player collides with the diamond and handle what to do when that happens in the code.

Once we have those components we can turn the gameobject into a prefab and put them across the 2D environment as we wish:

Creating the loot system

Now, in order to create our loot system, let’s start by creating a new C# script that we’ll attach to the diamond prefab:

Once created, let’s open the script and:

  • Include the System namespace at the top.
  • Tell the script to require a Rigidbody 2D and a Collider 2D to avoid having to check if these components are included in the gameobject.

Then, let’s create a new public static Action delegate that will serve as a way to notice each time that a diamond gets collected:

If you want to know more about Action delegates you can visit the Microsoft docs:

Then, let’s use the OnTriggerEnter2D method to check when another collider collides with the diamonds. Once that happens let’s:

  • Check if the tag from the other collider belongs to the player (as we want the player to collect the diamonds).
  • Check if the Action delegate has someone subscribed to the event to execute it.
  • Destroy the diamond gameobject.

Player script

Once we attached the Diamond script to the prefab, let’s open the Player script and create a new variable that will keep the count of the diamonds collected:

Use [SerializeField] or a public variable to be able to see when a diamond gets collected through the inspector.

Finally, let’s use the Start or Awake method to subscribe the player to the Action delegate from the Diamond script. This way we can use an anonymous method that will add a single diamond to the variable we created:

For more reference about the anonymous methods you can visit the Microsoft docs:

And now, if we run the game in the Unity editor, we’ll see that the diamonds get collected when the player collides with them:

The value of the diamonds variable changes each time that a diamond gets collected.

And that’s it, we created a loot system that allows the player to collect diamonds in our 2D game with Unity! :D. I’ll see you in the next post, where I’ll be showing a tutorial about UI events in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).