Here is the code I've been trying to use to serialize a list into JSON:
void Save()
{
Debug.Log("Save");
InfoSaveList saveList = new InfoSaveList();
// this steps through information nodes and collects
// the information they contain
foreach (BaseNode n in rootNode.childrenNodes)
{
var id = n.nodeID;
var info = n.infoLine;
InfoSave infoSaveData = new InfoSave();
infoSaveData.nodeID = id;
infoSaveData.info = info;
saveList.infoSave.Add(infoSaveData);
}
string infoSaveDataToJson = JsonUtility.ToJson(saveList, true);
Debug.Log(infoSaveDataToJson);
}
[System.Serializable]
public class InfoSave
{
public int nodeID;
public string info;
}
[System.Serializable]
public class InfoSaveList
{
[SerializeField] public List<InfoSave> infoSave;
}
for some reason I'm getting the error:
NullReferenceException: Object reference not set to an instance of an object
on the line:
saveList.infoSave.Add(infoSaveData);
I can't figure out why that would be the case, I'm trying to step through my code and it all seems to make sense to me but I'm clearly missing something.
If i get through this error, will this even save correctly as JSON? or can that only be done with arrays no lists?