Heya, So I’m trying to build an inventory system in C#, And so far its mostly working, Its just whenever I have only One object and remove it from the Inventory I get the error
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour{
public List<BaseItem> inventory = new List<BaseItem>();
//Reference Data
// Ui display Objects
bool _Isactive;
public GameObject InventoryMenu;
public GameObject ItemButton;
public GameObject InventoryDisplay;
public Text ItemNameDisplay;
public Text ItemDescDisplay;
//Equipment Slots
public BaseItem HeadSlot;
public BaseItem ChestSlot;
public BaseItem LegSlot;
public BaseItem BootSlot;
public BaseItem WeaponSlot;
// Base Item used For item Transference
void Start () { }
void Update () {
if (Input.GetKeyDown (KeyCode.Tab)) {
_Isactive =! _Isactive;
if (_Isactive == true) {
InventoryMenu.SetActive (true);
PopulateList ();
}
if (_Isactive == false) {
ClearDisplayList ();
InventoryMenu.SetActive (false);
}
}
}
public void PopulateList(){
for (int i = 0; i < inventory.Count; i++) {
int Index = i;
GameObject button = (GameObject)Instantiate (ItemButton);
button.GetComponentInChildren<Text> ().text = "" + inventory *.itemName;*
button.GetComponent<Item_descDisplay> ().Item_Name = “” + inventory .itemName;
button.GetComponent<Item_descDisplay> ().Item_Desc = “” + inventory .itemDesc;
* button.transform.SetParent (InventoryDisplay.transform,false);*
* button.GetComponent ().onClick.AddListener (delegate {*
* ActivateItem (Index);*
* });*
* }*
* }*
* public void ActivateItem(int I){*
_ if (inventory .ItemType == BaseItem.itemType.Head) {
* if (HeadSlot.itemName == “”) {
HeadSlot = inventory ;
print (“” + I);
inventory.RemoveAt (I);
rebuildList ();
}
if (HeadSlot.itemName != “”) {
inventory.Add (HeadSlot);
HeadSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
}
if (inventory .ItemType == BaseItem.itemType.Chest) {
if (ChestSlot.itemName == “”) {
ChestSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
if (ChestSlot.itemName != “”) {
inventory.Add (ChestSlot);
ChestSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
}
if (inventory .ItemType == BaseItem.itemType.Legs) {
if (LegSlot.itemName == “”) {
LegSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
if (LegSlot.itemName != “”) {
inventory.Add (LegSlot);
LegSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
}
if (inventory .ItemType == BaseItem.itemType.Weapon) {
if (WeaponSlot.itemName == “”) {
WeaponSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
if (WeaponSlot.itemName != “”) {
inventory.Add (WeaponSlot);
WeaponSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
}
if (inventory .ItemType == BaseItem.itemType.Boots) {
if (BootSlot.itemName == “”) {
BootSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
if (BootSlot.itemName != “”) {
inventory.Add (BootSlot);
BootSlot = inventory ;
inventory.RemoveAt (I);
rebuildList ();
}
}
}
void rebuildList(){*_
* foreach (Transform children in InventoryDisplay.transform) {Destroy (children.gameObject);}*
* PopulateList ();*
* }*
* void ClearDisplayList(){*
* foreach(Transform children in InventoryDisplay.transform)*
* {*
* Destroy(children.gameObject);*
* }*
* }*
}
It also seems that if I have multiple objects in the list it will select the next object( and example would be when I click a button set to the object at index 0 and it effects the object set to index 1)
I searched through The Code But I cant seem to find the problem, Ive spent about 2-3 days trying to fix it But Its beyond me.