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 ![]()
