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 01, 2014 at 07:33 PM · keydictionaryindex

KeyNotFoundException: The given key was not present in the dictionary

When ever i Call this Function i get the Error:

"KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.Int32,Item].get_Item (Int32 key)" on the line indicated by 's.

Code:

 void CraftItem(int craftedItemID, int R1ID, int R2ID, int R1Amount, int R2Amount){
             //Resource 1
             for(int i = 0; i < PlayerInventory.Inventory.Count; i++){
                 if(PlayerInventory.Inventory[i].ID == R1ID && PlayerInventory.Inventory[i].currentStack >= R1Amount){
                     PlayerInventory.Inventory[i].currentStack -= R1Amount;
                 } else if(PlayerInventory.Inventory[i].ID == R2ID && PlayerInventory.Inventory[i].currentStack >= R2Amount){
                     PlayerInventory.Inventory[i].currentStack -= R2Amount;
                 }
             }
             for(int cnt = 0; cnt < PlayerInventory.ItemDictionary.Count; cnt++){
                 if(PlayerInventory.ItemDictionary[cnt].ID == craftedItemID){************** 
                     Item craftedItem = PlayerInventory.ItemDictionary[cnt];
                     AddItem(craftedItem);
                 }
             }
         }

Whats wrong with what i am doing?

Comment
Add comment · Show 3
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 larku · Jan 01, 2014 at 10:23 PM 0
Share

You are using a dictionary like it is a list: Change your dictionary to a List

The inventory (dictionary) count is the number of elements in the dictionary (keys) but the key values do not necessarily have the values 0..PlayerInventory.Inventory.Count

avatar image stuart6854 · Jan 01, 2014 at 11:20 PM 0
Share

I change it to a List, but it has messed up ALOT, and i mean alot of my code :(

avatar image stuart6854 · Jan 01, 2014 at 11:59 PM 0
Share

Ok, will sort everything out tomorrow.

Thanks

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by larku · Jan 01, 2014 at 11:56 PM

You are using a dictionary like it is a list: Change your dictionary to a List

The inventory (dictionary) count is the number of elements in the dictionary (keys) but the key values do not necessarily have the values 0..PlayerInventory.Inventory.Count

You'll need to adjust all of your code to use the list as it's supposed to be used.

Think about how the Dictionary class works - the key can be any arbitrary value in the key type range where as the List class's index is an ordered set of values.

That is, the Dictionary keys can be any type (even strings) so there is no implied order.

You need to read up and understand the difference between a dictionary and a (generic) list in order to understand how they differ and when each is applicable.

Comment
Add comment · 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

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

iTween Paths : Element with the same key already exists in the dictionary 5 Answers

Dictionary wont give me value with correct key 1 Answer

Storing XML node and accessing from a dictionary 0 Answers

C# Auto Generated Key with Variable (Hash, Metadata, etc.) 2 Answers

The Given Key cannot be found in the Dictionary and I have no idea why. Worked perfectly before. 0 Answers


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