Error : ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name : index

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

public class PlayerInventory : MonoBehaviour
{
[Header(“General”)]

public List<itemType> inventoryList;
public int selectedItem;
public float playerReach;
[SerializeField] GameObject throwitem_gameobject;

[Space(20)]
[Header("Keys")]
[SerializeField] KeyCode throwItemKey;
[SerializeField] KeyCode pickItemKey;

[Space(20)]
[Header("Item gameobjects")]
[SerializeField] GameObject Wooden_Sword_item;
[SerializeField] GameObject Iron_Sword_item;

[Space(20)]
[Header("Item Prefabs")]
[SerializeField] GameObject Wooden_Sword_prefab;
[SerializeField] GameObject Iron_Sword_prefab;

[SerializeField] Camera cam;

private Dictionary<itemType, GameObject> itemSetActive = new Dictionary<itemType, GameObject>() { };
private Dictionary<itemType, GameObject> itemInstantiate = new Dictionary<itemType, GameObject>() { };

void Start()
{
    itemSetActive.Add(itemType.Wooden_Sword, Wooden_Sword_item);
    itemSetActive.Add(itemType.Iron_Sword, Iron_Sword_item);

    itemInstantiate.Add(itemType.Wooden_Sword, Wooden_Sword_prefab);
    itemInstantiate.Add(itemType.Iron_Sword, Iron_Sword_prefab);

    NewItemSelected();
}

void Update()
{
    Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    RaycastHit hitInfo;

    if (Input.GetKeyDown(throwItemKey) && inventoryList.Count > 1)
    {
        Instantiate(itemInstantiate[inventoryList[selectedItem]], position: throwitem_gameobject.transform.position, new Quaternion());
        inventoryList.RemoveAt(selectedItem);

        if (selectedItem != 0)
        {
            selectedItem -= 1;
        }
        NewItemSelected();
    }

        if(Physics.Raycast(ray, out hitInfo, playerReach) && Input.GetKey(pickItemKey))
    {
        IPickable item = hitInfo.collider.GetComponent<IPickable>();
        if (item != null)
        {
            inventoryList.Add(hitInfo.collider.GetComponent<ItemPickable>().itemScriptableObject.item_type);
            item.PickItem();
        }    
    }

    if (Input.GetKeyDown(KeyCode.Alpha1) && inventoryList.Count > 0)
    {
        selectedItem = 0;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha2) && inventoryList.Count > 0)
    {
        selectedItem = 1;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha3) && inventoryList.Count > 0)
    {
        selectedItem = 2;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha4) && inventoryList.Count > 0)
    {
        selectedItem = 3;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha5) && inventoryList.Count > 0)
    {
        selectedItem = 4;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha6) && inventoryList.Count > 0)
    {
        selectedItem = 5;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha7) && inventoryList.Count > 0)
    {
        selectedItem = 6;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha8) && inventoryList.Count > 0)
    {
        selectedItem = 7;
        NewItemSelected();
    }
    else if (Input.GetKeyDown(KeyCode.Alpha9) && inventoryList.Count > 0)
    {
        selectedItem = 8;
        NewItemSelected();
    }
}

private void NewItemSelected()
{
    Wooden_Sword_item.SetActive(false);
    Iron_Sword_item.SetActive(false);

    GameObject selectedItemGameobject = itemSetActive[inventoryList[selectedItem]];
    selectedItemGameobject.SetActive(true);
}

}

public interface IPickable
{
void PickItem();
}

you then call NewItemSelected - and expect inventoryList to have an index of -1 …

so do i just remove the newitemselected?

Well, not necessarily - is this your code or some code you got off a tutorial/pasted from somewhere, are you understanding what it actually does?

Im not entirely sure that setting selectedItem to -1 is the right answer if selectedItem is not 0, surely it might be -1 if there is no item? however, if in the newitemselected there is no current item such as it is -1 then it cant select a new item, only maybe unselect the last…

Unfortunately its your code, you know what you need it to do, we dont. you asked why it threw the error, ive explained to you why, because you set it to -1 and then called the method, this may or may not be the required behavior, if it is, the method potentially needs an update, if it isnt then well the Update needs fixing to stop that from being called …

thank you , and answering your fisrt question it is actually not my code and i got it off of a tutorial while trying to make an inventory system

thats OK often we all start somewhere, but Ive outlined the 2 ways to prevent this, but this needs to be your choice.

1 Like