KeyNotFoundExpection error

Like the title says I always get the KeyNotFound error.

The strange thing is that I use the code in the same script, but different void to control other characters and it works fine. The error script:

Node[] outgoingNodes = currentLocation.nodes;

            List<SCPnodeData> validNodes = new List<SCPnodeData>();

            for (int i = 0; i < outgoingNodes.Length; i++)
            {
                SCPnodeData temp = nodeName2Data[outgoingNodes[i].name];
                if (temp.weight == true)
                {
                    validNodes.Add(temp);
                }
            }

This code should define the next move of the character and the script should add nodes where the character can go to (called validNodes).

The error message tells me that the error appears on line 7.

How can I now try to solve this problem. Like whats the best approach for my problem?

Key not found. I assume nodeName2Data is a dictionary. So you don’t have whatever value you are trying to use to retrieve. Basically outgoingNodes*.name doesn’t exist in your dictionary.*
So, debug outgoingNodes*.name and then see why that value is not in your dictionary as expected.*
This error should be as easy to solve as a null error, since the concept is the same. You need to figure out what value is not found, why it’s not found, and then correct it.

1 Like