PLEASE someone to give me a code or help me!!!
Since 2 weeks I am trying different Binary systems from youtube tutorials and no one works for me
Here is the last one:
SaveSystem
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class SaveSystem : MonoBehaviour
{
#region Instance
private static SaveSystem instance;
public static SaveSystem Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<SaveSystem>();
if (instance == null)
{
instance = FindObjectOfType<SaveSystem>();
instance = new GameObject("Spawned SaveSystem", typeof(SaveSystem)).GetComponent<SaveSystem>();
}
}
return instance;
}
set
{
instance = value;
}
}
#endregion
[Header("Logic")]
[SerializeField] private string savefileName = "saves.s";
private PlayerData data;
public PlayerData Data { get => data; set => data = value; }
private BinaryFormatter formatter;
void Awake ()
{
formatter = new BinaryFormatter();
DontDestroyOnLoad(this.gameObject);
}
public void SaveData()
{
if (Data == null)
{
Data = new PlayerData();
var file = new FileStream(savefileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
formatter.Serialize(file, Data);
file.Close();
Debug.Log("Data Saved");
}
}
public void LoadData()
{
try
{
var file = new FileStream(savefileName, FileMode.Open, FileAccess.Read);
Data = (PlayerData)formatter.Deserialize(file);
file.Close();
Debug.Log("Data Loaded");
}
catch
{
Debug.LogError("Save file not found in " + savefileName);
SaveData();
}
}
}
The data that I want to save and load:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class PlayerData
{
public float coins { set; get; }
public bool isLaserBought { set; get; }
}
P.S. I want to be able to ‘‘interact’ with the data’ to get and set it
I would highly recommend something human readable like JSON to make your save system. It’s 10x easier to debug issues because you can just open the file and see at a glance what’s being written to the file. With binary saves you have to pull out a hex editor to tell what’s going on and even then it’s really easy to miss things
You try and access saveSystem.Data before you save or load it, so saveSystem.Data is null. Maybe you should be calling saveSystem.LoadData in GameManagerScript’s Awake or Start?
You’re not doing yourself any favors with how little information you are providing. You say you ran the above and it “doesn’t really save”. Well what does it “really” do specifically? LoadData has Debug lines, so which one was output to the console?
you can just use the inspector to mess around with it
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class savetest : MonoBehaviour
{
public BinaryFormatter bf = new BinaryFormatter();
public FileStream fs;
public int Coins;
public bool TriggerSave;
public bool TriggerLoad;
[Serializable]
public class SaveClass
{
public int Save_Coins;
}
public void SaveMethod()
{
if (Directory.Exists(Application.dataPath + "/Save/") == false)
{
Debug.Log("creating dir");
Directory.CreateDirectory(Application.dataPath + "/Save/");
}
fs = File.Create(Application.dataPath + "/Save/saved.coins");
sv = new SaveClass();
sv.Save_Coins = Coins;
bf.Serialize(fs, sv);
fs.Close();
}
public void LoadMethod()
{
if (File.Exists(Application.dataPath + "/Save/saved.coins") == true)
{
fs = File.Open(Application.dataPath + "/Save/saved.coins", FileMode.Open);
sv = (SaveClass)bf.Deserialize(fs);
Coins = sv.Save_Coins;
fs.Close();
}
}
void Update()
{
if (TriggerSave == true)
{
SaveMethod();
TriggerSave = false;
}
if (TriggerLoad == true)
{
LoadMethod();
TriggerLoad = false;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class savetest : MonoBehaviour
{
public BinaryFormatter bf = new BinaryFormatter();
public FileStream fs;
public SaveClass sv;
public int Coins;
public bool TriggerSave;
public bool TriggerLoad;
[Serializable]
public class SaveClass
{
public int Save_Coins;
}
public void SaveMethod()
{
if (Directory.Exists(Application.dataPath + "/Save/") == false)
{
Debug.Log("creating dir");
Directory.CreateDirectory(Application.dataPath + "/Save/");
}
fs = File.Create(Application.dataPath + "/Save/saved.coins");
sv = new SaveClass();
sv.Save_Coins = Coins;
bf.Serialize(fs, sv);
fs.Close();
}
public void LoadMethod()
{
if (File.Exists(Application.dataPath + "/Save/saved.coins") == true)
{
fs = File.Open(Application.dataPath + "/Save/saved.coins", FileMode.Open);
sv = (SaveClass)bf.Deserialize(fs);
Coins = sv.Save_Coins;
fs.Close();
}
}
void Update()
{
if (TriggerSave == true)
{
SaveMethod();
TriggerSave = false;
}
if (TriggerLoad == true)
{
LoadMethod();
TriggerLoad = false;
}
}
}