Write List<string> to file?

I’ve been having problems trying to save a particular List into a file. The particular code bellow gives me this output:

"
local: 3 vs file: 3
HELP! There’s no data!
"

If I were to load the same file using another script, the words list would come out as Null and I’d get an “NullReferenceException” error.

[System.Serializable]
public struct SaveStuff {
public List<string>words;
}

void SaveFile() {
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();

//Invent stuff to put on file
List<string> tempWords = new List<string>();
tempWords.Add("One");
tempWords.Add("Two");
tempWords.Add("Three");

SaveStuff saveData;

//Open the file just in case I need to write merge data later on
using (Stream filestream = File.Open(fileName, FileMode.Open, FileAccess.Read)) {
saveData = (SaveWorld)formatter.Deserialize(filestream);
}

//Add data to the save variable
if (tempWords.Count > 0) {
saveData.words = new List<string> ();
for (int i = 0; i < tempWords.Count; i++) {
saveData.words.Add (tempWords[i]);
}
UnityEngine.Debug.Log ("local:" + tempWords.Count + " vs file:" + saveData.words.Count);
}

//save data to file
using (Stream filestream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) {
formatter.Serialize(filestream, saveData);
}

if (saveData.words == null) {
UnityEngine.Debug.Log ("HELP!  There's no data!");
}
}

I’m not sure how to fix this. It seems pretty straight forward to me and I can save Lists of custom classes List no problem (so long as I set them up as [Serializable]); just not List ? Any input/clues would be grateful.

I thought Lists were serializable.

The solution was to make a temporary variable, make the changes, then apply the content to the original variable. I find it odd that Unity doesn’t give out an error for this.

Fixed:

[System.Serializable]
public struct SaveStuff {
public List<string>words;
}

void SaveFile() {
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter ();

//Invent stuff to put on file
List<string> tempWords = new List<string>();
tempWords.Add("One");
tempWords.Add("Two");
tempWords.Add("Three");

SaveStuff saveData;

//Open the file just in case I need to write merge data later on
using (Stream filestream = File.Open(fileName, FileMode.Open, FileAccess.Read)) {
saveData = (SaveWorld)formatter.Deserialize(filestream);
}

SaveStuff tempData = saveData;

//Add data to the save variable
if (tempWords.Count > 0) {
tempData.words = new List<string> ();
for (int i = 0; i < tempWords.Count; i++) {
tempData.words.Add (tempWords[i]);
}
UnityEngine.Debug.Log ("local:" + tempWords.Count + " vs file:" + tempData.words.Count);
}

saveData = tempData;

//save data to file
using (Stream filestream = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) {
formatter.Serialize(filestream, saveData);
}

if (saveData.words == null) {
UnityEngine.Debug.Log ("HELP!  There's no data!");
}
}

Line 23 and 34 are the ones you want to look at.