- Home /
Looping through List and the Last Item doesn't work.
I Have a list(below) that i am looping using a for loop, but for some reason unknown to me, when i use the loop to reference the last item it comes up with an error:
"ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"
This error comes out on the last loop with lines that hold this:
"PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount]."
Code:
LOOP
 for(int Craftcount = 0; Craftcount < Crafting.Craftables.Count; Craftcount++){
             print (Craftcount + " : CraftCount");
             GUI.BeginGroup(new Rect(10, 10 + (150 * Craftcount), 220, 150));
             GUI.Box(new Rect(0, 0, buttonWidth, buttonHeight), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].ItemID].Icon);//Icon 
             GUI.Label(new Rect(75, 0, 80, 40), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].ItemID].Name);//Item Name 
             GUI.Label(new Rect(75, 25, 140, 60), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].ItemID].Description);//Item Description
             GUI.Label(new Rect(0, 75, 120, 40), "Required Resources");//Resource Header
             if(CheckResourceAvailable(Crafting.Craftables[Craftcount].Resource1ID, Crafting.Craftables[Craftcount].Resource1Amount)){
                 GotR1 = true;
                 GUI.skin = greenSkin;
                 GUI.Label(new Rect(0, 95, 80, 40), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].Resource1ID].Name + " X " + Crafting.Craftables[Craftcount].Resource1Amount, "label");//Resource 1
             } else{
                 GotR1 = false;
                 GUI.skin = redSkin;
                 GUI.Label(new Rect(0, 95, 80, 40), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].Resource1ID].Name + " X " + Crafting.Craftables[Craftcount].Resource1Amount, "label");//Resource 1
             }
             if(Crafting.Craftables[Craftcount].Resource2ID != 99999){
                 if(CheckResourceAvailable(Crafting.Craftables[Craftcount].Resource2ID, Crafting.Craftables[Craftcount].Resource2Amount)){
                     GUI.Label(new Rect(90, 95, 80, 40), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].Resource2ID].Name + " X " + Crafting.Craftables[Craftcount].Resource2Amount, "label");//Resource 2
                     GotR2 = true;
                 } else{
                     GUI.skin = redSkin;
                     GUI.Label(new Rect(90, 95, 80, 40), PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].Resource2ID].Name + " X " + Crafting.Craftables[Craftcount].Resource2Amount, "label");//Resource 2
                     GotR2 = false;
                 }
             }
 
             GUI.skin = mySkin;
             if(GotR1 && (GotR2 || Crafting.Craftables[Craftcount].Resource2ID == 0)){
                 if(GUI.Button(new Rect(0, 115, 200, 25), "Craft " + PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].ItemID].Name)){//Working Craft Button
                     //Craft and Add Item to Inventory!!!!!!!!!!!!
                     int Iid = Crafting.Craftables[Craftcount].ItemID;
                     CraftItem(Iid, Crafting.Craftables[Craftcount].Resource1ID , Crafting.Craftables[Craftcount].Resource2ID , Crafting.Craftables[Craftcount].Resource1Amount, Crafting.Craftables[Craftcount].Resource2Amount);
 
                 }
             } else{
                 GUI.Button(new Rect(0, 115, 200, 25), "Craft " + PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].ItemID].Name, "box");//Faded-Out Craft Button
             }
             GUI.EndGroup();
         }
         GUI.EndScrollView();
         //GUI.DragWindow();
     }
List: This is the two Lists that i use:
 private    void AddItemsToArrays(){
         //InventoryItems//
         Item Wood = new Item(){
             Name = "Wood",
             ID = 0,
             Description = "",
             currentStack = 1,
             maxStack = 50,
             Icon = transform.GetComponent<PlayerInventory>().woodIcon,
             type = Item.Type.Resource,
         };
         PlayerInventory.ItemDictionary.Add(Wood);
 
         
         Item Stone = new Item(){
             Name = "Stone",
             ID = 1,
             Description = "",
             currentStack = 1,
             maxStack = 50,
             Icon = transform.GetComponent<PlayerInventory>().stoneIcon,
             type = Item.Type.Resource,
         };
         PlayerInventory.ItemDictionary.Add(Stone);
 
         Item Axe = new Item(){
             Name = "Axe",
             ID = 2,
             Description = "",
             currentStack = 1,
             maxStack = 1,
             Icon = transform.GetComponent<PlayerInventory>().axeIcon,
             type = Item.Type.Tool,
         };
         PlayerInventory.ItemDictionary.Add(Axe);
 
         Item Chicken = new Item(){
             Name = "Chicken",
             ID = 3,
             Description = "",
             currentStack = 1,
             maxStack = 25,
             Icon = transform.GetComponent<PlayerInventory>().chickenIcon,
             type = Item.Type.Food,
         };
         PlayerInventory.ItemDictionary.Add(Chicken);
         
         Item woodWall = new Item(){
             Name = "Wood Wall",
             ID = 4,
             Description = "Used in the Making of Houses.",
             currentStack = 1,
             maxStack = 5,
             Icon = transform.GetComponent<PlayerInventory>().woodWallIcon,
             type = Item.Type.Construction,
         };
         PlayerInventory.ItemDictionary.Add(woodWall);
         
         Item woodDoor = new Item(){
             Name = "Wood Door",
             ID = 5,
             Description = "Add a Lock and your House is safe!",
             currentStack = 1,
             maxStack = 1,
             Icon = transform.GetComponent<PlayerInventory>().woodDoorIcon,
             type = Item.Type.Construction,
         };
         PlayerInventory.ItemDictionary.Add(woodDoor);
 
         Item wooStairs = new Item(){
             Name = "Wood Stairs",
             ID = 6,
             Description = "Used to make multi-story buildings.",
             currentStack = 1,
             maxStack = 1,
             Icon = transform.GetComponent<PlayerInventory>().woodDoorIcon,
             type = Item.Type.Construction
         };
         
         //Craftable Items//
         CraftableItem _woodWall = new CraftableItem(){
             CraftID = 0,
             ItemID = 4,
             Resource1ID = 1,
             Resource1Amount = 4,
             Resource2ID = 99999,
             Resource2Amount = 0
         };
         
         CraftableItem _woodDoor = new CraftableItem(){
             CraftID = 1,
             ItemID = 5,
             Resource1ID = 0,
             Resource1Amount = 3,
             Resource2ID = 1,
             Resource2Amount = 1
         };
 
         CraftableItem _woodStairs = new CraftableItem(){
             CraftID = 2,
             ItemID = 6,
             Resource1ID = 0,
             Resource1Amount = 8,
             Resource2ID = 99999,
             Resource2Amount = 0
         };
 
         Crafting.Craftables.Add(_woodWall);
         Crafting.Craftables.Add(_woodDoor);
         Crafting.Craftables.Add(_woodStairs);
     }    
 
I am referencing the 'ItemDictionary' using the 'ItemID' from the current Item of 'Craftables'.
If their is anything you need me to show/tell you please ask.
Thanks for any help!
Answer by robhuhn · Jan 02, 2014 at 10:45 AM
I would say you're missing to add the last item 'wooStairs' to the dictionary:
    Item wooStairs = new Item(){
      Name = "Wood Stairs",
      ID = 6,
      Description = "Used to make multi-story buildings.",
      currentStack = 1,
      maxStack = 1,
      Icon = transform.GetComponent<PlayerInventory>().woodDoorIcon,
      type = Item.Type.Construction
    };
    //this line is missing:
    PlayerInventory.ItemDictionary.Add(wooStairs);
So PlayerInventory.ItemDictionary[Crafting.Craftables[Craftcount].ItemID] points to PlayerInventory.ItemDictionary[6] which is not present in the array.
Thanks, their is always a tiny mistake i have to make.
Thanks again
Your answer
 
 
             Follow this Question
Related Questions
A node in a childnode? 1 Answer
Storing different scripts into a list iterating issue 2 Answers
What type of array should I use to handle a convoy? 0 Answers
Assign a GameObject to one from a List<> 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                