Inventory system (with categories) got me a little bit confused...

Hello,

I am designing an inventory system for my game. I have watched many tutorials but none really fits what I am trying to achieve so I come for a little bit of help here.

My character can hold 4 types of items:

  • Katanas
  • Food
  • Outfits
  • Special items

So I have an inventory.cs script attached to my player with the 4 arrays of 20 objects (can hold no more than 20):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public GameObject[] katanas = new GameObject[20];
    public GameObject[] food = new GameObject[20];
    public GameObject[] outfits = new GameObject[20];
    public GameObject[] special = new GameObject[20];
}

Now, in my inventory UI, I have 4 pages of 20 buttons based on the 4 categories of object my player can hold. The UI looks like this:

Where the ROW A is for the categories managing the GRID like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TabGroup : MonoBehaviour
{
    public List<TabButtonGroup> tabButtons;
    public Sprite tabIdle;
    public Sprite tabHover;
    public Sprite tabActive;
    public TabButtonGroup selectedTab;
    public List<GameObject> objectsToSwap;

    public void Subscribe(TabButtonGroup button)
    {
        if(tabButtons == null)
        {
            tabButtons = new List<TabButtonGroup>();
        }

        tabButtons.Add(button);
    }

    public void OnTabEnter(TabButtonGroup button)
    {
        ResetTabs();
        if (selectedTab == null || button != selectedTab)
        {
            button.background.sprite = tabHover;
        }
    }
    public void OnTabExit(TabButtonGroup button)
    {
        ResetTabs();
    }
    public void OnTabSelected(TabButtonGroup button)
    {
        selectedTab = button;
        ResetTabs();
        button.background.sprite = tabActive;
        int index = button.transform.GetSiblingIndex();
        for(int i=0; i<objectsToSwap.Count; i++)
        {
            if (i == index)
            {
                objectsToSwap[i].SetActive(true);
            }
            else
            {
                objectsToSwap[i].SetActive(false);
            }
        }
    }

    public void ResetTabs()
    {
        foreach(TabButtonGroup button in tabButtons)
        {
            if(selectedTab!=null && button == selectedTab) { continue; }
            button.background.sprite = tabIdle;
        }
    }

}

Now, what I would like to do is fill the inventory grids based on what is in the player’s arrays from inventory.cs.

Knowing that each object that will be present in the arrays are scriptable objects, like my katanas for example, having a serialized field “category” string:

[SerializeField]
    private string category;

I am confused on how to achieve this and would really appreciate some help!
Thanks in advance :slight_smile:

The easiest way to do it in my opinion would be to have a class that manage each grid cell. I.e. : GridCell.

The GridCell class would be on each of the 20 cells. Your inventory would have a reference to each of these cells. When you switch tabs, you go through each cells and tell them to either switch to their new item or to become an empty slot based on what you player has. The GridCell has to have a slot for an Item scriptable object. When you give or remove item, the GridCell reads from this slot and refreshes their sprite.

Example : Oh, you are on the food tabs so your grid cells display 10 carrots and 10 empty slots. You click to swap to Katana’s tab. Your inventory script iterates through the grid cells list. Since the player has 5 katanas, it tells the 5 first slots to become a katanas, and the 15 others to become empty slot.

One way to achieve this would be to add the 20 grid cells references to your TabGroup script and have your TabGroup script hold a reference to the player inventory.
Hope this helped a bit !