Create a very simple Inventory using Scriptable Object in unity!

Hola everyone!

Today I will try to show you guys how to make a very simple inventory system using c# in unity. I will use Scriptable object very simply so that any beginner user also can understand clearly what is going on. Its not the best way to make an inventory but it will be simple to understand. Lets jump in……

I am assuming whoever is reading this at least have the basic idea of using Unity editor. So start by opening a new project.

The final project will look like this..

There will be buttons to select what kind of items i want to see..

The hierarchy will look like this,

Here what i did is:

  • Added panels to store the item cards and the buttons
  • Made a prefab of a single item card and added a ItemInfoLoader script to that object.
  • Made an scriptable object “ItemDataSO” to make instances.
  • Created some Item instances of the Scriptable Object and populate those values.
  • Populate the ItemHolder gameobject with ItemPrefab with the created ScriptableObject instances.
  • In the ItemHolder gameobject added an script ItemHolder to show and filter out the items.

Adding the panels:

There are 2 panels, One for Item cards holder “ItemHolder” and another for the buttons holder.

In the ItemHolder object added Horizontal layout group and a ItemHolder script to show and filter out items.

And in the button holder object “ItemTab” added a vertical layout group. Then populated the ItemTab with different buttons.Each button will have an on click event from “ItemHolder” script.

Making of the Item Card prefab:

Make the card UI gameobject with name and image in it .Then add the itemInfoLoader and put the ItemName and ItemImage in proper field. And add the ItemDataSO scriptable objects variants to make different items. Then just populate the itemHolder object and drag all objects in the itemHolder script attached with ItemHolder game object.Then make all items inactive.

Here are the scripts:

ItemDataSO.cs
[CreateAssetMenu]
public class ItemDataSO : ScriptableObject
{
    public string itemName;
    public Sprite itemImage;
    public Catagories itemCatagory;
}

public enum Catagories
{
    All,
    Burger,
    Pizza,
    Drinks,
    Soup
}
ItemHolder.cs
public class ItemHolder : MonoBehaviour
{
    public List<ItemInfoLoader> items;

    //private Catagories itemCatagory;


    private void Start()
    {
        LoadItems("All");
    }

    public void CatagoryChange(string itemCatagory)
    {
        LoadItems(itemCatagory);
    }

    public void LoadItems(string catagory)
    {
        Debug.Log(catagory);
        for (int i=0;i<items.Count;i++)
        {
            if (catagory.Equals("All"))
            {
                items[i].gameObject.SetActive(true);
            }
            else if (items[i].itemDataSo.itemCatagory.ToString()==catagory)
            {
                Debug.Log(items[i].itemCatagory);
                items[i].gameObject.SetActive(true);
            }
            else
            {
                items[i].gameObject.SetActive(false);
            }
            //Debug.Log(items[0].name);
            
        }
    }
    
}
ItemInfoLoader.cs
public class ItemInfoLoader : MonoBehaviour
{
    public TextMeshProUGUI itemName;
    public Image itemImage;
    public string itemCatagory;
    public ItemDataSO itemDataSo;

    private void Awake()
    {
        itemName.text = itemDataSo.itemName;
        itemImage.sprite = itemDataSo.itemImage;
        itemCatagory = itemDataSo.itemCatagory.ToString();
    }
}

That’s it.. If you need the project source code or any help or any other queries then comment here.

Thanks you all………

Endless Existence
Endless Existence
Articles: 74

Leave a Reply

Your email address will not be published. Required fields are marked *