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.

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:

  1. Decide when the box is destroyed.
  2. Compile a list of objects you want to create.
  3. Get the location of the box to be destroyed.
  4. Destroy the box using the Object.Destroy() method.
  5. 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);
        }
    }
}