- Home /
KeyNotFoundException that makes no sense (*I* can find the key)
I don't get it, what am I doing wrong? Or why is the compiler suddenly being stupid? I've used Dictionaries of all different types before without any trouble .
var letterToWords : Dictionary.<int[], List.< GameObject > >;
function RemoveLetter (where : int[]) { // does the deleted letter destroy one or more words, or form new words?
Debug.Log ("where:" + "["+where[0]+","+where[1]+","+where[2]+"]");
Debug.Log ("letterToWords.Count: " +letterToWords.Count);
var debugKeys : List.<int[]> = letterToWords.Keys.ToList();
var n : int = debugKeys.Count;
while (n--) {
Debug.Log ("key "+n+": ["+debugKeys[n][0]+","+debugKeys[n][1]+","+debugKeys[n][2]+"]");
}
Debug.Log ("letterToWords [where]: " +letterToWords[where]);
and here is the output...
where:[71,0,85]
letterToWords.Count: 11
key 10: [71,0,92]
key 9: [71,0,91]
key 8: [71,0,90]
key 7: [71,0,89]
key 6: [71,0,88]
key 5: [71,0,87]
key 4: [71,0,86]
key 3: [71,0,85] <---look! there it is! right there!
key 2: [71,0,84]
key 1: [71,0,83]
key 0: [71,0,82]
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.Int32[],System.Collections.Generic.List`1[UnityEngine.GameObject]].get_Item (System.Int32[] key) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
WordMatrix.RemoveLetter (System.Int32[] where) (at Assets/_Scripts/Base/WordMatrix.js:331)
LetterMatrix.Update () (at Assets/_Scripts/Base/LetterMatrix.js:150)
I'm stumped. I also put in debug messages where the key-value pair is assigned, and it seems to be working right on that end...
lm.SelectCell (dx,dy,dz);
var sC : int[] = new int[3];
sC[0] = dx;
sC[1] = dy;
sC[2] = dz;
//Debug.Log ("letterList.Add("+sC[0]+","+sC[1]+","+sC[2]);
letterList.Add (sC);
if (letterToWords.ContainsKey (sC) ) {
if (!letterToWords[sC].Contains (wordObj)) {
letterToWords[sC].Add (wordObj);
}
}
else {
var ltwl : List.<GameObject> = new List.<GameObject>();
ltwl.Add (wordObj);
letterToWords.Add (sC,ltwl);
}
lm.affectedCells.Add (sC);
lm.ChangeCell (dx,dy,dz, wordToPut[l]);
lm.ChangeNeighbors();
lm.BakeMeshes();
Debug.Log ("letterToWords.Count" + letterToWords.Count);
Debug.Log ("letterToWords[" +sC[0]+","+sC[1]+","+sC[2]+"][0]:" + letterToWords[sC][0]);
}
and get this output to the console, as I would expect :
...
letterToWords.Count4
letterToWords[71,0,85][0]:ABRACADABRA (UnityEngine.GameObject)
...
You are trying to use an int[] as a key. I don't think this will work the way you want it to.
Two int arrays are only equal if they are the same array. Two different arrays with the same contents are NOT == and thus one will not retrieve from the dictionary an entry keyed with another.
If you want two different arrays with the same values to be key compatible, you need to define your own struct and override Equals.
Ok, I guess that explains it. l don't like that it works that way, but now I know.
You should convert your comment to an answer, so I can accept it as correct.
Your answer
Follow this Question
Related Questions
Nested Functions 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
is resarting unity to update scripts a bug or an error on my part? 0 Answers
; expected. Insert a semicolon at the end. 1 Answer
Script error help! 1 Answer