- Home /
Save string and list to binary file
I want to save a string and class list to a binary file to be opened and instantiated into a prefab within another project. The string worked fine, but the list not so much. Whenever I try to access the Count part of the deserialized list, it gives me an error Object reference not set to an instance of an Object. When I debug out the list it shows null so I'm thinking it's on the save side, not the load side plus the whole file is only 1k. The list is 35 items long.
[Serializable]
SaveManager Class
public string Info;
public list<class>MyList;
Save script
FileStream fs = new FileStream("save.dat", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, save);
fs.Close();
Load script
if (File.Exists (Application.persistentDataPath + "/save.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/save.dat", FileMode.Open);
string info=file.Info;
classlist=file.MyList;
file.Close ();
}
Where am I going wrong here? Thanks!
Answer by Gilead7 · Sep 11, 2019 at 02:31 PM
I figured it out! First, I had to serialize the class that the list was created from on both projects. Then I looked and saw that I never even added the list the the binary file. So I added it and it worked! Then I went to the other project, serialized that and added the list=save.list and it worked! I was able to instantiate without an issue. This was way easier than I thought it would be. I hope this helps someone else that might be struggling with this too.
Yes, please share the final code please! I am stuck at saving list!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Add value to List without replacing previous 0 Answers
Binary file Deserialize to list? 0 Answers
A node in a childnode? 1 Answer