• 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 Mijdax · Jan 16, 2013 at 09:32 PM · destroyinventoryitemslot

Destroy Item Visuals but store a version of it

Hi guys. I am trying to create an Inventory which fills after gathering items like Coins etc.

My problem is that whenever I Destroy() the GameObject of the coin after adding it to an Array in my Inventory Component it will also miss the reference to the stored GameObject in the Array.

Code:

 collision.gameObject.GetComponent(Inventory).AddItem(this.gameObject);
 Destroy(this.gameObject);


 //Add Item to the First free Slot in the Inventory
 //Overload 1: Add Item to a Specific slot in the Inventory
 function AddItem(item:GameObject):void
 {
     AddItem(item,GetFirstFreeItemSlot());
 }
 
 function AddItem(item:GameObject, slot:int):void
 {
     //Check if there is a Item in that Specific Slot already
     if(slot >= 0 && itemSlots[slot] == null)
     {
         itemSlots[slot] = item;
         print("Inventory: The Inventory now contains: " + itemSlots[slot].GetComponent(ObtainableItem).itemName);
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by doublethink · Jan 16, 2013 at 10:01 PM

I am currently working on a solution to the exact same problem. The way I accomplished it is to have the GameObject I am destroying contain a member variable of a custom class I created that stores all of the information about that object. This may include the name of the object, name of the prefab used and any other information I may need. Also instead of using an array or list I created an Inventory script that has this member

 Dictionary<System.Guid, InventoryItem> _inventory;

that I can use to store the item information for later reference such as re-creating the object I have previously destroyed. Using System.Guid as the key means I don't have to spend time maintaining the list and I can have an unlimited number of instances of the same or different objects. This Dictionary can further be used in a InventoyGUI script that displays the items to the player for selection.

Comment
Add comment · Show 1 · 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 Mijdax · Jan 20, 2013 at 12:24 PM 0
Share

Thank you for your reply but I dont seem to be able to make your suggested solution work in my Project. isn't there any way to destroy the item without deleting reference to it in the variable? in other words storing a full copy of the given GameObject in it in my variables rather than linking to it

avatar image
0

Answer by Mijdax · Jan 20, 2013 at 08:29 PM

for those who are curious how I solved it:

I set both the renderer and the collider to false (invisible and i cant collide with it anymore)

 this.gameObject.renderer.enabled = false;
 this.gameObject.GetComponent(Collider).enabled = false;

Thats not really resource saving but yeah it works. I might optimize it later on

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Best approach in making Inventory UseItem functionality. 1 Answer

Custom Editor Window to Create and Store Item Data 1 Answer

How i can make a script-made button interactable? 0 Answers

Placing a gameObject into, and pulling out of inventory 2 Answers

Implementing an Item system - Defining Items 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges