2D Inventory System

Pretty much title says it all. I’m trying to implement a basic Inventory system where:
Player can pick up items from the scene
Picked up items get stored in the inventory
Selected items can be used and once they are used they are removed from the inventory.
My problem is that I can’t really seem to decide what is the best method. I’ve been searching around and pretty much a lot of posts mention that there aren’t specific ways to do this it really is a matter of how YOU want to achieve it. That being said, I have a new way but let me explain my previous way.

Previous way (didn’t work, couldn’t get it to work properly)

  • Idea was to create a list which takes gameObjects of those items and store them in the list. The list acts as the inventory so 15 elements means that there are 15 items in the list. Problem was that each time I picked up the item, I wanted to destroy the item from the scene but I couldn’t achieve this because as I added the gameObject to the list, then destroyed the gameObject from the scene, since the added listItem is referencing gameObject from the script, destroying the script destroys the gameObject from the inventory. Essentially I’m adding a null each time. After much trying, I got no where and ended up doing the same thing in different ways.

New Way (hoping to this)

  • I choose the previous way to start with is because I wanted the picked up Items from the scene to be deleted for the game to be ‘efficient’ in having/managing items in the scene. I realized that I barely have too many items at the moment in my scene so the new way would simply be hiding items using:

    GetComponenet.enabled = true/false

I just want to know whether there would be some sort of impact if I just have items in the scene then just hiding them and showing them when the player selects that item from the inventory.

Take The Forest as an example. Built in Unity, has an Inventory system. I’m wondering if the items that the player has are the items that have been moved from Position A (point of origin) to Position B (players inventory space)?

TL;DR - Just want to know whether hiding/showing is better than deleting and recreating objects when it comes to inventory system and picking up etc.

Hello,

Not sure how useful this would be but from experience i have used both. If you have a lot of items, then the show/hide way wouldnt really work. The best thing to do in my opinion is to tag every item of a type, and once you pick it up add it to the array and then display the array in the inventory layout of your choice. This way you arnt deleting anything, youre just moving things between game space and your array, and it also helps when managing items in your inventory. For example not having to make the object invisible manually each time. It also means that the engine wont have to render out any calculations on an object that isnt there. Try adding items on top of each other and having one visible with the rest invisible. Just isnt tidy!

So check out adding items you pick up to an array, and display the contents of the array when you want to view your inventory!

Like i said this is the way ive done it in the past, and its been pretty resource efficient and easy to implement and maintain. Feel free to ask away though, there are loads of ways of doing this!