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.

--

--

--

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Deploy A Backend App As An Android Engineer

Build Raspberry Pi Hadoop/Spark Cluster from scratch

A Flutter Web Framework

Programming’s Impact On Brain

Basic commands: Diving into Docker-Part 4

navigation

Tech Stack Roundup #3: April 2017

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Simon Truong

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.

More from Medium

How to Create A Loading Scene in Unity

How to Make the Camera Shake Using Impulse Listener Extensions in Cinemachines Virtual Cameras

How To Use Post Processing In Unity

3D Survival FPS Game