Troubles with inventory system

So I want to make an inventory system, simply because I have never done it before, I thought of all the things I would like it to do, to equip weapons from your inventory and to collect items from dead enemies, also to destroy items…

First up I made an item class, fairly simple with one stat - damagevalue, it’s icon as a sprite and it’s ID which I will explain later on why it is useful.

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

[System.Serializable]
public class Item{
  

    public int ID;
    public GameObject itemObject;
    public Sprite Icon;
    public float DamageValue;

}

Then I made a Slot Script for storing the items within slots in my bag, the UI of the bag consists of a panel on which I have placed on a 4x4 table made up from “Image” UI elements. Each slot has an Image, which is like a reference to the Image appropriate to the specific slot. As well as an item, which is the item that the slot contains;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Slot {
    public Item SlotItem;
    public Image SlotImage;
}

Now I have an itemdatabase script, which is actually just a script in which i have a list of all available items, as a List of the “Item” class.

Last of all, is the script that I am having problems with, the Inventory script itself:

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

public class Inventory : MonoBehaviour {
    public ItemDatabase dbRef; //A reference to the itemdatabase script
    public List<Slot> BagSlots = new List<Slot>(); //List of all the slots in my bag
    bool invUpdated;
    void Start(){
        dbRef = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<ItemDatabase> (); //Setting the reference through the itemDB tag
    }
    void Update(){
      UpdateInventory()

    }
    void UpdateInventory(){
        //This function is used as an update to get called all the time, it is actually what controls the slots of the specific item

        for(int i = 0; i< BagSlots.Count; i++){ //Starting from the first element in BagSlots, till the last
            BagSlots[i].SlotItem = dbRef.itemDatabase[BagSlots[i].SlotItem.ID]; // So I am setting each item to be equal to the Item with the index 'ID'
                                                                                //The ID of an item is used for me to find the item in the database..

            BagSlots [i].SlotImage.sprite = BagSlots [i].SlotItem.Icon; //In this line I am just changing the Sprite, the "icon" in the UI;       

        }
    }
      

}

Now unity throws me an error “ArgumentOutOfRangeException” each time the loop happens and I can’t figure out why

I think you’re confusing arrays and lists in how you’re adding your items.

You add to a list with yourListName.Add(whateveryouwanttoadd);

I’m also not sure what BagSlots.Count is meant to represent in your code here, unless you’re setting the size somewhere else, but even then, I think it’s best you look at some examples of using lists and storing classes in a list.

Here’s one example: c# - Storing data into list with class - Stack Overflow

I am not trying to add an item in this script, what I’m doing is checking and updating each slot of my inventory bag to change it’s icon if the item inside the slot has changed, for example i deleted an item from my inventory so the slot that contained the item should now display a blank image.

However, I will convert these lists into arrays to see if it works better this way

And I just converted the lists into arrays, everywhere in which there was “BagSlots.Range” I converted to “BagSlots.Lenght” and it is still giving me the
“ArgumentOutOfRangeException: Argument is out of range.Parameter name: index” error. What I think would be most crucial in solving this is if someone could tell me what that error actually means ?

Hum… How did you initialize the array?

Well, I would just ditch this entirely and try an Event approach. It’ll be much better.
When you add/delete an item (or move) it in your inventory, it’s then that you should update what’s there, its icon, etc… :slight_smile:

Even if your code had been working, I would have suggested this. :slight_smile: