Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by KelseySHock · Jul 05, 2018 at 11:06 AM · dictionary

Key not in Dictionary?

Here's my dictionary:

 public Dictionary<string, int> foodWeight = new Dictionary<string, int>();
     foodWeight.Add("cake", 1);

and here's where the issue occurs (its at the first foodWeight line):

         for (int i = 0; i < foods.foodItems.Length; i++)
         {
             if (foods.currentHungerPoints >= 1400
                 && inventory.weight + foodWeight[foods.foodItems[i]] < 30
                 && inventory.size + foodSize[foods.foodItems[i]] < 40)
             {
                 numOfCakes += 1;
                 inventory.weight += foodWeight[foods.foodItems[i]];
                 inventory.size += foodSize[foods.foodItems[i]];
                 Destroy(other.gameObject);

Here is my foods script pertaining to foodItems:

public string[] foodItems; foodItems = new string[2]; foodItems[0] = "cake";

Edit: I've debugged the crap out of this and I still have no clue. It will literally work correctly if I put in 0, but if I make the for loop put in the 0 it conks out! Edit: This is my full error message if that changes anything --> KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.String,System.Int32].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) InventoryItems.OnTriggerStay (UnityEngine.Collider other) (at Assets/Player/InventoryItems.cs:44)

Comment
Add comment · Show 11
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 donutLaserDev · Jul 05, 2018 at 11:39 AM 0
Share

If you get the error that the key is not in the dictionary, it means whatever key you are passing into the accessor is definitely not there, otherwise you wouldn't get the error. I don't see anything wrong with the posted code, which means the problem might be somewhere else. Perhaps, foodItems[0] = "cake" is called later than the loop. Perhaps something overrides the foodItems[0]. I suggest debugging the script and seeing what is the value of foods.foodItems[i] in each loop iteration.

avatar image KelseySHock donutLaserDev · Jul 05, 2018 at 11:47 AM 0
Share

This is literally so wild! I put Debug.Log(foods.foodItems[i]); before my if statement in my for loop and it literally spits out cake despite it not being able to find cake in the next line. (and I have foodItems[0] = "cake" in my awake function so it can't be that either). Such a conundrum. Thank you for your input!

avatar image donutLaserDev KelseySHock · Jul 05, 2018 at 11:54 AM 0
Share

You know what, it's not foodWeight that doesn't have the "cake" key. It's probably foodSize that doesn't have that key. Edit: you should post the actual error you get.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Perfecter · Jul 05, 2018 at 10:28 PM

The error is here.

 public string[] foodItems; 
 foodItems = new string[2]; 
 foodItems[0] = "cake";

You allocated array for two strings but assigned only first element. The second element will have default value for string (it is empty string = ""). Then, when you are in your for loop, you gets empty string and trying to get value with this key (="") from foodWeight dictionary. Just set proper foodItems array:

 public string[] foodItems;
 foodItems = new string[] {"cake"};

 




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

89 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 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 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 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 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

Using a dictionary of two enums 0 Answers

Dictionary ContainsKey not working with Vector3s? 0 Answers

change variable from textAsset in dictionary 1 Answer

Selecting between user-made levels 0 Answers

Enemy Database using list/dictionary to store stats (C#)? 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