- Home /
How to Serialize and Deserialize a Dictionary Containing Gameobject and a Vector
Hii,
I am making a Level Editor for a game that can edit levels in unity's edit mode. It saves progress in the end which can be loaded back when the game is running, but turns out you cannot serialize Unity's Gameobject or any custom C# object by unity (Even if it says so). I was able to serialize most of the stuff but stuck with one Dictionary object.
Now, here is the problem.
[HideInInspector] public Dictionary<GameObject, Vector2> occupants;
This Contains a Dictionary of a GameObject and a Vector2 (Vector 2 is the position in 2d grid and not 2d space). None of it is serializable by the normal method.
public static void SaveDataValues(DataValuesToSave dataValues, string level)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + level + ".dat";
FileStream stream = new FileStream(path, FileMode.Create);
DataValuesToSave data = dataValues;
formatter.Serialize(stream, data);
stream.Close();
}
How can I serialize this Dictionary? Any suggestions would be appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613030429im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Help] Dictionary bug 2 Answers
Serialization of GameObject 1 Answer
From two lists into a dictionary... and back? 5 Answers
does this key not found error have to do with serialization? 2 Answers