Inventory -Argument is out of range - Diagnostics to be done

Hi im trying to create an inventory and when adding a new slot and an item i get the Argument is out of range error. also when i try and add 2 items at the same time only the first one goes through.

void Update(){
		if (Input.GetKeyDown (KeyCode.Space)) {
			if (inTrigger = true) {
				if (TriggerTag.Equals ("AppleTree")) {
					Collect ("Apple", "An Apple", 1, null, Item.ItemType.Consumeable);
				}
			} else {
				Debug.Log ("There is nothing to collect!");
			}
		}
	}

 void Collect(string itemName,string itemDescription,int itemAmount,   Sprite itemIcon, Item.ItemType type){
    		this.GetComponent<Inventory> ().AddItem (itemName,itemDescription,itemAmount,itemIcon,type);
    	}

The above passes in some information you need for an item to be added
and now to the next script

public void AddItem (string itemName,string itemDescription,int itemAmount,   Sprite itemIcon, Item.ItemType type){
		if (inventory.Count <= InventoryMaxSize) {
			if (inventory.Count == 0) {
				inventory.Add (new Item (itemName, itemDescription, itemAmount, itemIcon, type));
				AddSlot ();
				UpdateGui();
			} else {
				for (int i = 0; i < inventory.Count; i++) {
					if (inventory *.ItemName == itemName) {*

inventory .ItemAmount = inventory .ItemAmount + itemAmount;
* UpdateGui();*
* break;*
* } else {*
* AddSlot();*
* inventory.Add (new Item (itemName, itemDescription, itemAmount, itemIcon, type));*
* UpdateGui ();*
* break;*
* }*
* }*
* }*
* }*
* }*

* public void AddSlot(){*
* GameObject slot = (GameObject)Instantiate (SlotGO);*
* Slots.Add (slot);*
* slot.transform.parent = GameObject.FindGameObjectWithTag (“InvGUI”).transform;*
* slot.GetComponent ().localPosition = new Vector3 (SlotStartPosX, SlotStartPosY, 0);*
* slot.GetComponent ().localScale = new Vector3 (1, 1, 1);*
* SlotStartPosY = SlotStartPosY - SlotMoveDown;*
* }*
* public void UpdateGui(){*
* for (int i = 0; i < Slots.Count; i++) {*
* for (int x = 0; i < inventory.Count; x++) {*
_ Slots .GetComponent ().item.ItemName = inventory [x].ItemName;
Slots .GetComponent ().item.ItemDescription = inventory [x].ItemDescription;
Slots .GetComponent ().item.ItemAmount = inventory [x].ItemAmount;
Slots .GetComponent ().item.ItemIcon = inventory [x].ItemIcon;
Slots .GetComponent ().item.itemType = inventory [x].itemType;
* }
}
}*

The error points to the UpdateGUI function. also if i call the collect function twice from the first script with different item names it only seams to add the first one. any help would be greatly appreciated_

1 Answer

1

In UpdateGui() method, control variable in for loop is increased but condition does not check it. Hence x continues to increase and eventually will lead to array index out of bounds error:

 for (int x = 0; i < inventory.Count; x++) 

try replacing with:

 for (int x = 0; x < inventory.Count; x++) 

Also, unclear why a second inner loop is needed since same Slots is being replaced multiple times.

thank you very much. silly mistake on my behalf haha:P guess looking at for ages it just slipped through.