After following a Brackey’s tutorial I’ve created a save and load system for my software, however, while testing it I’ve found it is only successfully saving a single integer, for some reason it’s not saving my array. This is a pretty big problem since I need to also save lists and even dictionaries. Please help me figure out what’s going on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class GameManager : MonoBehaviour
{
// Variables
// Misc Variables
public int ValueInt;
public string[] ValueStringArray = { };
// Misc Controllers
public void ReadIntInput(string number)
{
// Sets Single Number Value
ValueInt = Convert.ToInt32(number);
Debug.Log("int saved");
}
public void ReadStringInput(string temporary)
{
// Adds Values To Temp Arrays
List<string> ValueStringList = new List<string>(ValueStringArray);
ValueStringList.Add(temporary);
ValueStringArray = ValueStringList.ToArray();
Debug.Log("string saved");
}
// Save System
public void SaveState()
{
SaveSystemNew.SaveUserData(this);
Debug.Log("Saved " + ValueInt);
foreach (string i in ValueStringArray)
{
Debug.Log("Saved " + i);
}
}
// Load Systems
public void LoadState()
{
SaveDataNew data = SaveSystemNew.LoadUserSave();
ValueInt = data.ValueInt;
Debug.Log("Loaded " + ValueInt);
foreach (string i in ValueStringArray)
{
Debug.Log("Loaded " + i);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SaveDataNew
{
public int ValueInt;
public string[] ValueStringArray = { };
public SaveDataNew (GameManager Info)
{
// Misc Variables
ValueInt = Info.ValueInt;
ValueStringArray = Info.ValueStringArray;
}
}
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystemNew
{
public static void SaveUserData (GameManager Info)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/testSaveNew.qul";
FileStream stream = new FileStream(path, FileMode.Create);
SaveDataNew data = new SaveDataNew(Info);
formatter.Serialize(stream, data);
stream.Close();
}
public static SaveDataNew LoadUserSave()
{
string path = Application.persistentDataPath + "/testSaveNew.qul";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
SaveDataNew data = formatter.Deserialize(stream) as SaveDataNew;
stream.Close();
return data;
}
else
{
Debug.LogError("Save File Was Missing From " + path);
return null;
}
}
}