- Home /
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.
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.
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.
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.
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.
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
Follow this Question
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