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 SamTebbs33 · Aug 23, 2014 at 10:14 PM · c#inventoryinventory system

Clone a component, add it to a list and destroy the GameObject

I'm creating an inventory system which consists of a List that holds a number of Items. I have so far been able to get the Item component of a GameObject and add it to the List and then destroy the game object (to simulate it being picked up), but this then destroys the Item in the list. Is there any way to store a script component of one game object in a variable, clone it (without adding to the scene) add it to List and then destroy the game object? I tried using Instantiate but this spawns the prefab into the world, which I wouldn't like to do.

Comment
Add comment · Show 1
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 AlwaysSunny · Aug 23, 2014 at 10:21 PM 0
Share

If you want to use instances of items, your items should derive from ScriptableObject

This will solve this and other problems associated with trying to use the monobehavior / component systems to drive your items and inventory. Seems like a pain, and will involve some refactoring to set up, but it's widely held that this is the "proper" way to have items and inventories.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Aug 23, 2014 at 10:21 PM

Components require a game object, so you cannot use a component to store information that you want to live on after the game object is destoryed. But if I understand what you are trying to do correctly, you can create a class not derived from MonoBehaviour. This would be a 'normal' class, not a component. You would create the instance of this class using the 'new' operator, initialize the class using a 'normal' constructor instead of Start(), and you would store a reference to this class in a class instance variable on a component of the game objects.. Then when you pick the object up, you can assign the reference to this 'normal' class to your list, then destroy the game object.

Alternately you could not worry about any destroying the game object, and disable the renderer and any colliders instead. You could even disable the game object. This would use a bit more space, but it would make it easy to drop the game object if that is part of your game.

Comment
Add comment · Show 3 · 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 AlwaysSunny · Aug 23, 2014 at 10:34 PM 0
Share

Yeah, what he said. You should abstract "items" that exist in your inventory from "items" that exist in the scene.

Essentially you want a SceneItem monobehavior which "has a" ItemData object, as opposed to the "is a" relationship you currently have.

avatar image SamTebbs33 · Aug 23, 2014 at 10:52 PM 0
Share

I like the second idea the most, as it will make dropping the item much easier. I'd only need to change the Transform data and then enable the GameObject! Thanks. I actually have a class called Item that stores things like Item names etc and an ItemStack class that stores things like stack sizes and a reference to the Item that it is storing.

avatar image Kiwasi · Aug 23, 2014 at 11:50 PM 0
Share

I just had an interesting concept of a sealed room off screen where all of you inventory items are stored, with all colliders and functionality still enabled. Could make a great concept for a fantasy game with magic items.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Issues with Inventory script 0 Answers

Count object list duplicates 1 Answer

Decorator pattern for inventory item class 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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