Loot System in Unity
I have added the attack system to allow the player to beat the enemies. It is now time to get loot from defeated enemies.
To start, I went through the process of animating the diamond that will be the loot. I added the first frame to the scene, added an animator to the diamond, and animated the diamond.
In the player script, create a variable to hold the number of diamonds the player has. Then create a method that will add the number of diamonds collected to the diamond variable.
Create a new script that will be on the diamond. This will have a reference to the player. a score variable, and a method that will take in the number of gems and add it to the score variable. The script will use an OnTriggerEnter2D method to detect the player and use the score as the number to update the player's diamond amount. The script will then delete the diamond.
Back in unity, add a collider to the diamond and make sure the diamond script is also on the diamond.
Now you can prefab the diamond.
In the enemy script, create a reference to the diamond prefab. Create a reference to the diamond script. Now create a method to instantiate the diamond, set the reference to the diamond script, and call the method in the diamond script using the value of the gem of the enemy.
Go into each enemy and add the SpawnDiamond() method where you call the death of the enemy.
Make sure to add the diamond prefab to your enemies and give them the correct gem values. This will spawn the diamond and give the player the correct amount of gems for the enemy defeated.