- Home /
Can't seem to find a good, working way of instantiating (dropping) items after they've been added to inventory
I got the basics of my item system working, however I can't figure out how to re-instantiate an item from the inventory into the world again in order to drop it.
My current problem is the fact that the prefab variable my ItemData has, is assigned to the instance of the prefab instead of the prefab, so when I destroy the Item GameObject and add the ItemData, the reference which I use to instantiate it is invalid.
The first line to the left, is the instance of the prefab in the bottom. The second line to the right shows what the Prefab variable is connected to.
And this picture shows how things look when I picked the item up into the inventory. Since the Item instance was deleted, the Prefab variable of the ItemData in the inventory is invalid. The thing is, I don't want it to reference the instance, I want it to reference the prefab.
What would be a good solution to this problem? I considered a database with all items, but that feels kinda overkill. (My game will most likely have a lot of items)
Here are the fixed working scripts, added them as links to pastebin:
ItemData.cs
Item.cs
Inventory.cs
Answer by TSI25 · Jan 31, 2018 at 04:34 PM
So probably what you want to do is have a serialized class or scriptable object that contains data about the 'item' - stuff like weight or price or anything else you need to track about it. any time this 'item' is in someone's inventory it doesn't necessarily need to exist in the scene, and you can retrieve any data you need from that.
Then you would have this item have a prefab reference to the physical object, so if they chose to drop the 'item' it would instantiate whatever its prefab is, and then be removed from their inventory list (or array or whatever). This prefab would then need a reference to whatever the item data is. if you are using scriptable objects you can set that as a reference, or if you are using a serialized class you can read the data in from the inventory item's data when you instantiate it. It is important that this prefab reference isn't assigned to an object in the scene because when that object is deleted youll get a missing reference like youre seeing there.
ultimately it comes down to maintaining a separation between the data of an inventory item, and the in-game physical representation of the item. This approach will allow you to have a ton of objects in a player's inventory, as well as retrieving and passing that data around without actually needing to have prefabs loaded in the scene - while still allowing you to instantiate prefabs from that data when needed. Let me know if that doesn't make sense.
I had no idea at all ScriptableObjects could be used as assets, I've been thinking they were just some weird Unity-alternative to normal classes. I'm not sure where I got that from, but this fixed the issue. Just had to change about 2 lines and everything started working. Thank you so much. I'll go ahead and add my code to the post in case anyone would like to check out how ti actually works. Again, thank you! :)