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 [i].ItemName == itemName) {
inventory [i].ItemAmount = inventory [i].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<RectTransform> ().localPosition = new Vector3 (SlotStartPosX, SlotStartPosY, 0);
slot.GetComponent<RectTransform> ().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 [i].GetComponent<SlotScript> ().item.ItemName = inventory [x].ItemName;
Slots [i].GetComponent<SlotScript> ().item.ItemDescription = inventory [x].ItemDescription;
Slots [i].GetComponent<SlotScript> ().item.ItemAmount = inventory [x].ItemAmount;
Slots [i].GetComponent<SlotScript> ().item.ItemIcon = inventory [x].ItemIcon;
Slots [i].GetComponent<SlotScript> ().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
Answer by btmedia14 · Jul 07, 2016 at 11:30 PM
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[i] 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.
Your answer
Follow this Question
Related Questions
ArgumentOutOfRangeException: Argument is out of range. -> Index 2 Answers
Unknown Argument Out of Range Index Error On Card Game 1 Answer
How do I get my Inventory Loading to work again? ArgumentOutOfRangeException 2 Answers
UNET ArgumentOutOfRangeException 0 Answers
Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers