Dictionary Trouble Accessing at Key

So I’m pretty new to dictionaries in Unity, and I’m having some troubles as the title suggests. I define two Dictionaries at the top of the code without issue:

public Dictionary<int, int> itemIDs = new Dictionary<int, int>();
public Dictionary<string, int> itemNames = new Dictionary<string, int>();

and assign the values & keys further along as part of a loop, before attempting to test the dictionaries. itemIDs works perfectly, however itemNames does not.

itemIDs.Add(int.Parse(sections[0]), inventory.Count - 1);
itemNames.Add(sections[2], int.Parse(sections[0]));

testing this works:

Debug.Log(itemIDs[20]);

testing this doesn’t:

Debug.Log(itemNames["dirt"]);

And throws the exception “The given key was not present in the dictionary.”

I’ve spent a while looking around for an answer, but I can’t seem to find any of what I’m doing wrong. I’ve also tested all the values I pass into the keys and values of the dictionaries, and all of them are as expected. If any of you know what I’m doing wrong and/or how to fix it, please let me know.

Thank you for your time,

zedseven

itemNames.Add(“dirt”, 1);
Debug.Log(itemNames[“dirt”];

Does that work? If so then sections[2] doesn’t contain “dirt”. It’s also case sensitive so “Dirt” != “dirt”.

Add a call to Debug.Log to log the value of sections[2] before you add it to the dictionary and see what values are being added.