Save and load inventory

I really dont know where to start, i really dont have any idea on how to save my inventory, I would really appreciate if someone just give me a hint or a bit knowledge on saving this inventory.

public class Inventory : MonoBehaviour {

    public List<GameObject> Slots = new List<GameObject>();
    public List<Item> Items = new List<Item>();
    public GameObject slots;
    ItemsDatabase database;
  
    public GameObject tooltip;
    public GameObject draggedItemGameObject;
    public Item draggedItem;
    public int indexOfDraggedItem;
    
    public bool draggingItem = false;
    
  

    void Update()
    {
       if(draggingItem)
        {
            Vector3 posi = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
            draggedItemGameObject.GetComponent<RectTransform>().localPosition = posi;
            
        } 
    }

    public void showTooltip(Vector3 toolPosition, Item item)
    {
       tooltip.SetActive(true);
        tooltip.transform.GetChild(0).GetComponent<Text>().text = item.ItemName;
       
        tooltip.transform.GetChild(2).GetComponent<Text>().text = item.ItemDesc;
    }
    public void closeTooltip()
    {
        tooltip.SetActive(false);
    }

    public void showDraggedItem(Item item, int slotNumber)
    {

            
            indexOfDraggedItem = slotNumber;
            closeTooltip();
            draggedItemGameObject.SetActive(true);
            draggedItem = item;
            draggingItem = true;
            draggedItemGameObject.GetComponent<Image>().sprite = item.ItemIcon;

            
        
    }

    public void closeDraggedItem()
    {
        draggingItem = false;
        draggedItemGameObject.SetActive(false);
    }

    int x = -60;
    int y = 101;

	void Start () {
     
        int SlotAmount = 0;
        database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemsDatabase>();

	    for (int i = 1; i < 5; i++)
        {
            for(int k = 1; k < 4; k++)
            {
                GameObject slot = (GameObject)Instantiate(slots);
                slot.GetComponent<SlotScript>().slotNumber = SlotAmount;
                Slots.Add(slot);
                Items.Add(new Item());
                slot.transform.SetParent(this.gameObject.transform);
                slot.name = "Slot" + i + "." + k;
                slot.GetComponent<RectTransform>().localPosition = new Vector3(x, y, 0);
                x = x + 60;
                if(k == 3)
                {
                    x = -60;
                    y = y - 50;
                }
                SlotAmount++;
            }
        }

        //additems to the slot, can be stacked(consumable)
        addItem(4);
        addItem(4);
        addItem(10);


        //addItem(11);




    }

    //stacking items in slot
    public void checkIfItemExist(int itemID, Item item) 
    {
        for (int i = 0; i < Items.Count; i++)
        {
            if (Items*.ItemID == itemID)*

{
Items_.ItemValue = Items*.ItemValue + item.ItemValue;*_

break;
}
else if (i == Items.Count - 1)
{
addItemAtEmptySlot(item);

}
}
}

public void addExistingItem(Item item)
{

if (item.itemType == Item.ItemType.Consumable)
{
checkIfItemExist(item.ItemID, item);
}

else
{
addItemAtEmptySlot(item);
}

}

void addItem(int id)
{

for (int i = 0; i < database.items.Count; i++)
{

if (database.items*.ItemID == id)*
{

//another if for “MATERIALS”
Item item = database.items*;*
if (database.items*.itemType == Item.ItemType.Consumable)*
{
checkIfItemExist(id, item);
break;
}
else
{
addItemAtEmptySlot(item);
}

}
}
}

void addItemAtEmptySlot(Item item)
{

for(int i = 0; i<Items.Count; i++)
{
if(Items*.ItemName == null)*
{
Items = item;
break;
}
}
}

Hi mate, I’m not sure if this is the best way of doing it, but you could use PlayerPrefs. You can save values to PlayerPrefs and well as read them. PlayerPrefs is just something designed to store data between sessions.

You can save to PlayerPrefs by doing the following (you can also .SetString if you need to save text values, instead of numerical data)

PlayerPrefs.SetInt("ItemsCount", currentInventoryCount);

Then to read back values you can do the following:

currentInventoryCount = PlayerPrefs.GetInt("ItemsCount");

Hope this helps abit :wink:

See Unity - Scripting API: PlayerPrefs for more details.