Modifying scene objects programatically references prefab

Hi,

I have a scene where I create gameobjects from a prefab in the Resources folder at runtime:

var runtimeObject =(GameObject)UnityEngine.GameObject.Instantiate(Resources.Load(actorsTemplate_[rr]));

These object have a dictionary member called private

private Dictionary<string, float> floatUpdate;

At runtime, I add some stuff to the dictionaries in this way:

public void SetFloatParameter(string Name, float value)
    {
        if (floatUpdate.ContainsKey("Name"))
            floatUpdate[Name] = value; // !!LINE #1
        else
            floatUpdate.Add(Name, value); // !!LINE #2
    }

The problem I’m facing is:

ArgumentException: An element with the same key already exists in the dictionary.

on the !!LINE #1 (as specified in the code block after // comment).

What could be causing this?

Thanks.

Hey there,

Your issue is the contains key check you are doing. You check to see if “Name” is a key. This will never be true unless you sent “Name” in with that function. Change “Name” to Name and you will be fine.

public void SetFloatParameter(string Name, float value)
     {
         if (floatUpdate.ContainsKey(Name))
             floatUpdate[Name] = value; // !!LINE #1
         else
             floatUpdate.Add(Name, value); // !!LINE #2
     }