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