- Home /
Need some advice for my inventory system
Hello,
I am fairly new to Unity and am working with 2 other people on a new game. I try to program it in a way so that the other 2 people I am working with can easily create new inventory items as they are no programmers. The thing is I am not sure if this is the way I should do it, or if there is a more obvious way to handle this.
First, the way I want my inventory system to work:
It will be pretty simple, items that can be picked up are placed in the scene by us, not randomly spawned or anything. When you pickup the item the item will be moved to your inventory. You can click the item in your inventory to "equip" the item in your hand and use it on something else (for example: use a key on a door).
What I have now:
(check the bottom for links to a couple of images and the code)
I have an abstract Interactable class which can be extended to make different interact types. I now have an InteractableButton (just a simple button) and an InteractablePickup, the InteractablePickup accepts an InventoryItem asset and on interact the InventoryItem gets placed in the inventory.
InventoryItem is a scriptableobject, with a name, description, sprite and prefab. Name, description and sprite is used for the inventory gui, the prefab is the object that will be instantiated in the hand.
At first I had 2 gameobjects, one that is placed in the world, which had a collider and the InteractablePickup script. The second one is used for the item held in hand. This way you had to create 3 things for every inventory item, 2 gameobjects and one InventoryItem asset. The 2 gameobjects look the same, but the only difference is that one had the InteractablePickup component (and maybe in the future other components what the object for the hand doesn’t need). I really didn’t like the idea of having 2 gameobjects which essentially have the same model and texture etc.
The way I fixed this is by making 1 prefab which doesn’t have the InteractablePickup script and collider. And in the scene I made an empty gameobject which have these 2 components and added my prefab as a child.
So the process to create an inventory item is as follow:
Make a new prefab, with the model and texture etc.
Make a new InventoryItem asset
Drag prefab into the InventoryItem asset
Create a new empty gameobject in the scene (I will probably make a prefab with the InteractablePickup script already attached)
Add the InteractablePickup script and a collider to the “empty” GO.
Add the prefab (from step 1) as a child
Drag the InventoryItem asset to the InteractablePickup component on the empty GO.
The part that I need an empty gameobject with the prefab as child still bugs me, and I wonder if there are some unseen problems that will arise later on. It is possible that I am totally overthinking this, but I wonder what you think of this and what I can do to improve this.
You can find a couple of images here http://imgur.com/a/aIVaV
And the code can be found here https://gist.github.com/jordanske/a4a4dd51015c1e5daabc5b1e07dabdad