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

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

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

Best Way to do Regular Items and Weapons/Intractable/Special Items. 1 Answer

Cannot convert from 'int' to 'PlayFab.ClientModels.ItemInstance' 1 Answer

Trying to make a simple inventory: 0 Answers


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