Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by stuart6854 · Jan 02, 2014 at 10:05 AM · listindexiterate

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stuart6854 · Jan 02, 2014 at 10:54 AM 0
Share

Thanks, their is always a tiny mistake i have to make.

Thanks again

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

For all elements above a certain index 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges