For this loot system we will use diamond gameobjects

Desired behavior:

Process:

  • Declare a variable int gemsAmount (I will do it in Enemy 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 an int gems variable
  • In Diamond script, in OnTriggerEnter2D (don’t forget the ‘2D’ letters, remember it’s a 2D game, and if not, the trigger behavior won’t work), and check if other is player. Add:

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

Unity developer