x


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.

more ▼

asked Aug 27 '10 at 07:16 PM

Simon 5 gravatar image

Simon 5
2 3 3 3

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

Aug 27 '10 at 09:57 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
        }
    }
}
more ▼

answered Aug 27 '10 at 10:05 PM

SteveFSP gravatar image

SteveFSP
1k 8 13 29

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 minimal 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.

Aug 28 '10 at 11:13 AM Simon 5

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 programming, 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.

Aug 28 '10 at 03:11 PM SteveFSP
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1675
x1095
x764
x67
x23

asked: Aug 27 '10 at 07:16 PM

Seen: 2104 times

Last Updated: Aug 27 '10 at 07:16 PM