Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question KeyNotFoundExpection error

Discussion in 'Scripting' started by getmyisland_dev, Aug 13, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    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:

    Code (CSharp):
    1. Node[] outgoingNodes = currentLocation.nodes;
    2.  
    3.             List<SCPnodeData> validNodes = new List<SCPnodeData>();
    4.  
    5.             for (int i = 0; i < outgoingNodes.Length; i++)
    6.             {
    7.                 SCPnodeData temp = nodeName2Data[outgoingNodes[i].name];
    8.                 if (temp.weight == true)
    9.                 {
    10.                     validNodes.Add(temp);
    11.                 }
    12.             }
    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?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
    getmyisland_dev likes this.