Help - KeyNotFoundException: The given key was not present in the dictionary ?

Hey guys, I am using this code to take float and fill it in my dictionary

public void SetFloat(string varID, float value)
{
if(floats.ContainsKey(varID))
{
floats[“varID”] = value;
}
else
{
floats.Add(varID, value);
}
}

and I am using this code to get back the values from the dictionary

public void GetFloat(string varID)
{
if(floats.ContainsKey(varID))
{
return(varID)
}
else
{
print(“It doesn`t contain such ID”)
}
}

but I am getting this error every time i call GetFloat(“ID”) function -

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary2[System.String,System.Single].get_Item (System.String key) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) SaveManager.GetFloat (System.String varID) (at Assets/Easy SaveLoad For Begginers/Resources/Scripts/SaveManager.cs:118) Sample.Load () (at Assets/Easy SaveLoad For Begginers/Resources/Scripts/Sample.cs:42) UnityEngine.Events.InvokableCall.Invoke (System.Object[ ] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144) UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621) UnityEngine.Events.UnityEventBase.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()

change this:

if(floats.ContainsKey(varID))
    {
        floats["varID"] = value;
    }

to this:

if(floats.ContainsKey(varID))
    {
        floats["varID"].Value = value;
    }
1 Like

is that your entire code for “GetFloat(string varID)”, because there is no return type, and you’re returning the string you’re passing in, not the associated float based on the string as the key for the dictionary…

which line is 118?

1 Like

Please wrap your source into code-tags.
This code snippet doesnt make sense:

public void GetFloat(string varID)
{
   if(floats.ContainsKey(varID))
   {
      return(varID)
   }
   else
   {
      print("It doesn`t contain such ID")
   }
}

Missing semicolons, completely wrong syntax, so I guess this is pseudo-code written by you to let us figure out the problem right?

You need to use the Dictionary-indexer to lookup the value associated with your varID key.
And I advise you to use Dictionary.TryGetValue for unsafe value accessing because it is more performance intensive to use Dictionary.ContainsKey.

public float GetFloat(string varId)
{
   float value = -1;
   if (!floats.TryGetValue(varId, out value))
      Debug.Log("A value stored with the key " + varId + " has not been found.");

   return value;
}

Edit: By the way, have you checked if you really added a new item with “ID” as the key? Maybe this SetFloat(“ID”) call is not executed because of an if-statement or serialization-errors.

1 Like

This is incorrect. The Dictionary-indexer does not have the returning type KeyValuePair.

1 Like

Thanks for mentioning that.
For future reference OP should format his posts like so:

1 Like

Oh yeah sorry but I don’t have proper coding tools with me to test at the moment.

1 Like

It should be:

  • floats[varID].Value = value;

Note passing varID rather than the literal “varID”.

1 Like

Hey guys, Finally the problem is resolved :slight_smile: ! Thanks to all of you !

Yes, its a pseudo code cause i was not posting it from my formal PC :slight_smile: I will wrap codes into tags, It was first Post, Please tell me how to do that :slight_smile:

http://forum.unity3d.com/threads/using-code-tags-properly.143875/

What the heck? That literally is what traderain proposed as a solution and we came to the conclusion that this is incorrect. Why repost it again?

1 Like

I was replying to post #2.
floats[“varID”] = value;
vs
floats[varID] = value;

wari already confessed that his code was not real but pseudo-code. So there is no need to correct syntax errors, and you need to address the quoted user to make it a quote, theres an inbuild feature to do this automatically.

I may have cross-posted with that. Also, nobody had mentioned the “varID” thing as opposed to the .Value thing, so I figured I’d mention it. No need to start an inquisition about it. :slight_smile:

Not sure what you mean about the quote thing.

Quoting somebody means you put in the quoted users name so that everybody knows that its a quote and to who you are referring to. And still, there is no need to correct the syntax error “varID” because it was nonexistant in the real source. This up there is just pseudo code. Written as an example of what the thread-poster had in his script.