Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges