KeyNotFoundExeption

Hello :slight_smile:

(Probably Hi Brian since usualy you are my rescuer :wave: )

After implementing dictionary I start getting this issue…

Errors pops up when I collect weapon and after I kill enemy.
As you can see - stas on UI (enemy health , XP and Level) stopped show up.

Can it be related to changes in character prefab? (I worked on this lately too)

Thank you for your help in advance - as always much appreciated!

Hey I’ve been working with Dictionarys a lot recently. This means that you’re assuming the key you’re wanting to use is already stored inside the dictionary. For example if you just create a dictionary like…

Dictionary<int, string> stringDictionary = new Dictionary<int, string>();

Then you do something like…

    void Start()
    {
        if (!stringDictionary[0].Contains("Hello"))
        {
            stringDictionary[0] = "Hello!";
        }
    }

It will cause an error stating “The given key was not present in the dictionary” because this key never existed in the first place inside the dictionary. It would be a different story if we already assigned index “0” to something else cause then we could say index 0 now equals to “Hello!”.

But I’m sure even if Brian comes in here he’s going to need to see your code…

2 Likes

Thank you Brandon!!!

It was issue with my enemy prefab…I added accidentally Experience.cs to it… :woozy_face:
after removing it from prefab everything works fine!

1 Like

That will do it, unless of course, you add an Experience Needed component… Who says only player shoudl level up? (kidding, of course)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.