Creating a Loot System in Unity

Ali Emre Onur
3 min readMay 24, 2021

--

In the Dungeon Escape game, there will be a shop that we can use to buy items. Each of the enemies killed will drop some diamonds that we can collect to be used in the shop.

The abstract enemy class that is being used by the different type of enemies already had a field namely “gems” — which is the amount of diamonds that will be collected by the Player once they are dead.

For this purpose, a prefab of Diamond has been created. Since all of the enemies inherit from the Enemy abstract class, it is better to use the Enemy class to create the Diamond field that will be instantiated once they are dead.

For the Diamond prefab, the initial quantity is defined as 1 in a public variable. This value will be accessed by the enemies once they are dead and will be replaced by the amoun of gems they hold.

Diamond Class
Enemy Class

For the player, I have defined a public variable of diamonds that will be reached by the Diamond script once it is collected by the player.

From the inspector, I have assigned values for the gems that each enemy is carrying.

The amount that an enemy is holding needs to update the Diamond class with the value of theirs. To make sure that we cannot kill an enemy multiple times (and collect diamonds each time we kill it while it is already dead), we make the implementation safe with the existing isDead bool variable.

And that’s it.

--

--

No responses yet