Creating a Loot System (part 2)
Instantiating “x” amount of diamonds upon enemy death
Continuing from part 1, we are going to allow the enemy upon death to spawn a preset amount of diamonds that varies between different types of monsters. (ie. the spider enemy would drop only 1 diamond, while the Moss Giant is going to drop 3 diamonds).
The Enemy Script:
Within the enemy script we have already create the variable “gems”. This variable will be the preset of how many diamonds are going to drop when defeated.
To instantiate the diamonds we first need to have a protected gameobject variable to house the diamond.
Then we are going to create a public virtual method that will give access to all the child scripts. We will named this public method “diamond Drop” which instantiate the amount of diamonds according to the preset “Gems” amount. At the same time, we will also make the Diamonds that spawn, instantiate 1 point to the right of the first initial spawn.
Using a for loop, we will create the condition which basically instantiates new diamonds until the set gems amount is hit.
In every enemy script (aka: Skeleton, Moss Giant and Spider) we are going to call the public method within our Damage method. When Health is below 1, we will trigger the “diamond Drop” method that will instantiate new diamonds until the set gem’s quota is filled.
When we test this new script out in Unity, we can see that upon enemy death, the set upon of diamonds will drop and stack side by side.