- Home /
monster dropped items
I'm making an rpg and i was wondering how to make a script for a monster to drop an item such as coins when it dies
Answer by skovacs1 · Sep 28, 2010 at 07:23 PM
There are different ways to go about this and the best solution would depend on your use case, but generally you create prefab for what you're dropping, and when you're dropping it, call Instantiate. A simple implementation is that in the 3D platformer tutorial's EnemyDamage script.
var coins : Transform; //a prefab to instantiate
function Update() { if( health <= 0) { //die Destroy(gameObject); Instantiate(coins, transform.position, Quaternion.identity); } }
How can you then delete the object when the player comes in contact with it?
The coin prefab should have a script on it that finds collision with a player, and repeats the Destroy(gameObject); function
Check out the second example at http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html - You just need to apply a similar script to the Coin and look at adding a collider trigger.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074146im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move a object to my position? 2 Answers
Monster AI patrol an idle 0 Answers
Pressing "Fire2" Spawns and removes object on click 0 Answers