Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Simon 5 · Aug 27, 2010 at 07:16 PM · instantiateobjectdestroyitempick

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skovacs1 · Aug 27, 2010 at 09:57 PM 0
Share

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

avatar image mentaldepth · May 07, 2015 at 07:34 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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:

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

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Simon 5 · Aug 28, 2010 at 11:13 AM 0
Share

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.

avatar image SteveFSP · Aug 28, 2010 at 03:11 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

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

how to raycast instantiated game characters 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges