Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PigStyle · Apr 22, 2019 at 12:02 AM · errorserializationvector2dictionary

The given key was not present in the dictionary, i am calling the key immediately after adding it

Here is the dictionaries i am calling from in the function

 public Dictionary<Vector2, GameObject> TilePos = new Dictionary<Vector2, GameObject>();
     public Dictionary<Vector2, GameObject> UnitPos = new Dictionary<Vector2, GameObject>();
     public Dictionary<Vector2, GameObject> BuildingPos = new Dictionary<Vector2, GameObject>();

Here is the function that the error originates from

 public void SaveMap(Dictionary<Vector2, GameObject> TP, Dictionary<Vector2, GameObject> UP,Dictionary<Vector2,GameObject> BP, string SaveName)
     {
         if (!System.IO.File.Exists(Application.dataPath + "/StreamingAssets/Saves/" + SaveName + ".dat"))
         {
             if (SaveName != "")
             {
                 if (!Regex.IsMatch(SaveName, @"^[a-z][A-Z]+$"))
                 {
                     Map save = new Map();
                     foreach (KeyValuePair<Vector2, GameObject> kvp in TP)
                     {
                         foreach (var kvvp in DBC.TerrainDictionary)
                         {
                             if (kvp.Value.transform.name == kvvp.Value.Title)
                             {
                                 save.TerrainPositions.Add(kvp.Key, kvvp.Value);
                             }
                         }
                     }
                     foreach (KeyValuePair<Vector2, GameObject> ukvp in UP)
                     {
                         if (ukvp.Value != null)
                         {
                             foreach (var keyvp in DBC.UnitDictionary)
                             {
                                 if (ukvp.Value.transform.name == keyvp.Value.Title)
                                 {
                                     save.UnitPosition.Add(ukvp.Key, keyvp.Value);
 
                                     //this errors, cant figure out why
                                     save.UnitPosition[ukvp.Key].Team = ukvp.Value.transform.GetComponent<SpriteController>().Team;
                                     //this errors, cant figure out why
                                 }
                             }
                         }
                     }
                     foreach (KeyValuePair<Vector2, GameObject> bkvp in BP)
                     {
                         if (bkvp.Value != null)
                         {
                             foreach (var kvp in DBC.BuildingDictionary)
                             {
                                 if (bkvp.Value.transform.name == kvp.Value.Title)
                                 {
                                     save.BuildingPositions.Add(bkvp.Key, kvp.Value);
                                     save.BuildingPositions[bkvp.Key].Team = bkvp.Value.transform.GetComponent<SpriteController>().Team;
                                 }
                             } 
                         }
                     }
                     string destination = Application.dataPath + "/StreamingAssets/Saves/" + SaveName + ".dat";
                     var fs = File.Create(destination);
                     BinaryFormatter bf = new BinaryFormatter();
                     bf.Serialize(fs, save);
                     fs.Close();
                     MEMCC.SaveFeedback.text = "File saved as: " + SaveName;  
                 }
                 else
                 {
                     MEMCC.SaveFeedback.text = "Only use letters";
                 }
             }
             else
             {
                 MEMCC.SaveFeedback.text = "Add a name.";
             }
         }
         else
         {
             MEMCC.SaveFeedback.text = "Current file name is already in use, please rename your save or delete old file.";
         }
     }

Here is the class for Map

 [Serializable]
 public class Map
 {
     public Dictionary<SeralizableVector2, Terrain> TerrainPositions = new Dictionary<SeralizableVector2, Terrain>();
     public Dictionary<SeralizableVector2, Unit> UnitPosition = new Dictionary<SeralizableVector2, Unit>();
     public Dictionary<SeralizableVector2, Building> BuildingPositions = new Dictionary<SeralizableVector2, Building>();
 }


This is my unique vector2 (unity vector2 are not serializable)

 [Serializable]
 public class SeralizableVector2
 {
     public float x;
     public float y;
 
     public SeralizableVector2(float rx,float ry)
     {
         x = rx;
         y = ry;
     }
 
     public static implicit operator Vector2 (SeralizableVector2 rValue)
     {
         Vector2 newVec = new Vector2(rValue.x, rValue.y);
         return newVec;
     }
 
     public static implicit operator SeralizableVector2(Vector2 Value)
     {
         return new SeralizableVector2(Value.x, Value.y);
     }
 }



Let me know if more info is needed. Any help is appreciated.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DiegoSLTS · Apr 22, 2019 at 03:15 AM

You have to override the Equals and GetHashCode methods in a class if you want to use instances of that class as keys of a Dictionary, or you can pass a IEqualityComparer when you create the Dictionary object.

Comment
Add comment · Show 3 · Share
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 PigStyle · Apr 22, 2019 at 12:03 AM 0
Share

Again, thank you if you can provide some insight, this is driving me nuts because I cannot see the problem for the life of me.

avatar image DiegoSLTS PigStyle · Apr 22, 2019 at 03:36 AM 1
Share

This is the official documentation of IEqualityComparer: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1?view=netframework-4.8

You must tell the dictionary hoy to compare to instances of the key Class, otherwise the Dictionary won't find a key even if you added it. Equals and GetHashCode (or the IEqualityComparer, which implements the same methods) are used by the dictionary. If you search "IEqualityComparer" on Google you'll find lots of examples.

avatar image PigStyle DiegoSLTS · Apr 23, 2019 at 02:59 AM 0
Share

i bow to you my master, now on to other bugs...... lol

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

139 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 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 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 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 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 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 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

does this key not found error have to do with serialization? 2 Answers

[Solved]How to serialize Dictionary with Unity Serialization System 6 Answers

Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers

Unity Serializer scripting error 1 Answer

unexpected convert float to int error 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