- Home /
Instantiating droppable items on destroy game object
Hi
I am trying to get items to appear when an object box is destroyed, I have tried looking at the enemy damage script in the lerpz tutorial for reference and I would just like to understand why the script only works with the copper prefab.
To be clear all I really want is to destroy a box and have items left in its place.
I am sure there is a simple reason and/or something I am missing, if someone could please help me understand this I would be most grateful.
Could you be more specific? The EnemyDamage script in the tutorial will work with anything, there is nothing specific to any prefab in that script. As long as you call the ApplyDamage function with greater than or equal to the hit points or even calling the Die function directly, whatever prefab you have set in the script attached to the gameObject instance will be instantiated in its place and the prefab drops that you set dropped
Hope you get this, I'm soo0o close, might figure it out before you answer. I can make the coin appear using the csharp method when I tick the little box that says destroy box under the inspector view while testing the game.. But when I kill the object that has the script attached the item doesn't show up.... its only when I destroy the box, not the enemy I attached the script to. How do I change it from destroy box to destroy enemy? s0o0o close..
Answer by SteveFSP · Aug 27, 2010 at 10:05 PM
I'll start off simple and edit if you need more clarification. There are multiple ways to accomplish what you are trying to do. This is one.
The basic steps are as follows:
- Decide when the box is destroyed.
- Compile a list of objects you want to create.
- Get the location of the box to be destroyed.
- Destroy the box using the Object.Destroy() method.
- Clone the objects from the list you compiled and place them near the location of the box. See the Object.Instantiate() method.
Steps 4 and 5 may need to be swapped, depending on your design.
Examples:
The following scripts can be attached to your box and the boxDestroyed variable updated by other scripts. Or you can incorporate the code into the script that currently controls the box's destruction.
Javascript Version
// The items in the box. // You assign these items to valid prefabs in the editor. // E.g. GunPreb, AmmoPrefab, etc. var items = new GameObject[3];
// Set this value to true to cause the box to be destroyed // and the items created. var boxDestroyed = false;
// Update is called once per frame function LateUpdate () { if (boxDestroyed) { var position = transform.position; // Clone the objects that are "in" the box. for (item in items) { if (item != null) { // Add code here to change the position slightly // so the items are scattered a little bit. Instantiate(item, position, Quaternion.identity); } } // Get rid of the box. Destroy(gameObject); } }
C# Version
using UnityEngine; using System.Collections;
public class BoxInventory : MonoBehaviour { // The items in the box. // You assign these items to valid prefabs in the editor. // E.g. LettucePreb, BeerPrefab, etc. public GameObject[] items = new GameObject[3];
// Set this value to true to cause the box to be destroyed
// and the items created.
public bool boxDestroyed = false;
// Update is called once per frame
void LateUpdate ()
{
if (boxDestroyed)
{
Vector3 position = transform.position;
// Clone the objects that are "in" the box.
foreach (GameObject item in items)
{
if (item != null)
{
// Add code here to change the position slightly
// so the items are scattered a little bit.
Instantiate(item, position, Quaternion.identity);
}
}
// Get rid of the box.
Destroy(gameObject);
}
}
}
Hi Steve, thank you for responding and for taking the time to write a script for me, just so you know my understanding of java script is $$anonymous$$imal at best (it took me 2 days to implement a score system!) so when I look at C# script i really don't get it at all, i am guessing that your script requires a function Apply Damage but that it is not written like that in C#. Thank you again for your help.
I've updated the answer with a Javascript version, but I'm going to avoid the temptation to expand the answer to cover multiple topics. The example scripts can be used in two ways: Add the script to the object and have another script update the boxDestroyed variable when the object is destroyed. Or incorporate the example code into the script that controls box destruction. Since you are new to program$$anonymous$$g, concentrate on understanding the core concepts of what the the example script is doing. After that it should be easier to figure out how to incorporate those concepts into your scripts.
Your answer
Follow this Question
Related Questions
Cannot destroy child object in prefab- Error 1 Answer
How to delete an instantiated object 0 Answers
Destroy the current GameObject? 7 Answers
Object won't instantiate at parents position(solved) 2 Answers