- Home /
Why can't I save an array?
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;
}
}
}
Answer by AUsername64 · Aug 27, 2021 at 09:50 PM
I'm dumb. Check LoadState, It doesn't contain a variable to load the saved data for the array.
It only contains
ValueInt = data.ValueInt;
When it should also contain
ValueStringArray = data.ValueStringArray;
Your answer
Follow this Question
Related Questions
[SOLVED] First array slot blocking second array slot 1 Answer
PlayerPrefs script problem 2 Answers
SerializationException: Type UnityEngine.Vector3 is not marked as Serializable. 1 Answer
Issues with string array taken from a .txt file 2 Answers
What is best way to save several arrays into a file? 0 Answers