I have recently started with Unity. Currently I’m stuck when it comes to data persistence. While I can create a file, however it doesn’t contain the serialized variable. I’ve already read and seen various tutorials. Unfortunately, I still do not get ahead. I would be very grateful if someone could help me.
This is my SaveLoad.cs I could use without editing much:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public static class SaveLoad {
public static List<Game> savedGames = new List<Game>();
public static void Save() {
savedGames.Add(Game.current);
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd");
bf.Serialize(file, SaveLoad.savedGames);
file.Close();
}
public static void Load() {
if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
file.Close();
}
}
}
Here is my game.cs with the (I hope) serialized variable I try to save:
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Game {
public static Game current;
public Click CurrentEuros { get; set; }
public Game () {
//CurrentEuros = 1;
CurrentEuros = new Click();
}
}
The outcome is alway in the last line of the savedGames.gd “Game”.