Create a Loot System in Unity
For this loot system we will use diamond gameobjects
Desired behavior:
After destroying the enemy, a diamond prefab will be instantiated. The player can trigger on it, it will be destroyed and increase the diamonds amount in the player.
Process:
- Declare a
public int diamonds
variable inPlayer
script - Declare a variable
int gemsAmount
(I will do it inEnemy
base class but it can be done in each enemy script) and add this code snippets:

It will force the child classes to implement SetGemsAmount()
- In each enemy:

- in
Damage
(in Enemy script),if (Health <1)
, add:

This will set the gems
value (Diamond) as the gemsAmount
value (Enemy)
- In
Diamond
script declare also anint gems
variable - In
Diamond
script, inOnTriggerEnter2D
(don’t forget the ‘2D’ letters, remember it’s a 2D game, and if not, the trigger behavior won’t work), and check ifother
isplayer
. Add:

This will increase the diamonds
value in player
and destroy the diamond prefab