• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kiloblargh · May 25, 2014 at 08:37 PM · javascripterrordictionary

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)

...

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · May 25, 2014 at 09:06 PM 1
Share

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.

avatar image Kiloblargh · May 26, 2014 at 01:57 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

is resarting unity to update scripts a bug or an error on my part? 0 Answers

Nested Functions 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

No appropriate version of 'UnityEngine.Object.Instantiate. Please Help :( 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges