- Home /
Can't get serialization Load/Save to work!C#
Thanks in advance .I can't seem to get my Save/Load script to work :(. I try to use Unity serialization for this ,but alas when I hit the Load button I strangely get the values that my Game Master had in the beginning, before I even made this script!?
Notes:
This script is attached to Game Master object.
This object travels between scenes using a singleton design pattern.
Please don't tell me to use Player Prefs, I plan to add more variables and I want to learn !!
Sorry, new to Unity Answers don't know how to post my script properly :/.
I have tried both using File.Create and File.Open for my save method, same result.
I have found the file on my computer, so the the save method does create one.
using UnityEngine; using System.Collections.Generic; using System.Linq; using UnityEngine.UI; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class SaveLoad : MonoBehaviour {
public static SaveLoad control;
public float Armour;
public int Credits;
public Text Score;
void Awake(){
if (control == null) {
control = this;
DontDestroyOnLoad (gameObject);
} else if (control != this) {
Destroy(gameObject);
}
}
void Update(){
if (Score == null) {
Debug.Log("No Score Counter!!");
}
control.Score.text ="Credits: " +Credits.ToString();
}
public static void KillPlayer(Player player){
Application.LoadLevel("Menu");
}
public static void KillEnemy(Enemy Enemy){
Destroy (Enemy.gameObject);
}
public void Save(){
if (File.Exists (Application.persistentDataPath + "GameInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open(Application.persistentDataPath + "GameInfo.dat", FileMode.Open);
Debug.Log (Application.persistentDataPath);
PlayerData data = new PlayerData ();
data.Armour = Armour;
data.Credits = Credits;
bf.Serialize (file, data);
file.Close ();
Debug.Log ("Save");
}
}
public void Load(){
Debug.Log (Application.persistentDataPath);
if(File.Exists(Application.persistentDataPath + "GameInfo.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "GameInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
Debug.Log ("Load");
file.Close();
Armour = data.Armour;
Credits = data.Credits;
}
}
[System.Serializable]
public class PlayerData{
public float Armour;
public int Credits;
}
}
Have yu tried inserting Debug.Log lines to see if the right values are saved and loaded inside the save and load function? Your script looks good to me at quick glance.
Great Idea, thanks!; I have noticed that when I save it ignores my current values and sets the data values to serialized to 6 and 20!!!!! Why could this be?
I wouldn't say that it "ignores the current values"; rather, it in fact takes the Armour and Credits values, but these are for some reason 6 and 20 (you can check this with more Debug.Log lines if it isn't apparent in the inspector). You probably set these variables to 6 and 20 before saving somewhere.
I just did...and it sees them correctly(also they show up in the inspector),but when I save ,it saves the base values only, i have now changed them to 0 and 0.
Answer by Garross · Aug 03, 2015 at 01:44 PM
Figured it out xD my save button was attached to a prefab of my Game Master !!!! Thank you all so much for your help and setting me on the right track.
Answer by Positive7 · Aug 02, 2015 at 07:39 PM
Application.persistentDataPath + "GameInfo.dat"
to
Application.persistentDataPath + "/GameInfo.dat"
Thanks ,however now that i have changed the script the Save/Load is working very strangely.... when I save my game I have 750 credits , when I load I have 20(a value that was used in the script previously to test the UI counter)????As well as that when I go back to menu from my Level 1 without saving , the load button does absolutely nothing, doesn't even call my Debug Lines?!
Update: I have debug.Logged my save values and I have noticed that for some wild reason it saves the data variables to be stored as 20 and 6 totally ignoring my current values from the game!
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveLoad : $$anonymous$$onoBehaviour {
public static SaveLoad control;
public float Armour;
public int Credits;
public Text Score;
void Awake(){
if (control == null) {
control = this;
DontDestroyOnLoad (gameObject);
} else if (control != this) {
Destroy(gameObject);
}
}
void Update(){
if (Score == null) {
Debug.Log("No Score Counter!!");
}
control.Score.text ="Credits: " +Credits.ToString();
}
public static void $$anonymous$$illPlayer(Player player){
Application.LoadLevel("$$anonymous$$enu");
}
public static void $$anonymous$$illEnemy(Enemy Enemy){
Destroy (Enemy.gameObject);
}
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/GameInfo.dat");
Debug.Log (Application.persistentDataPath);
PlayerData data = new PlayerData ();
data.Armour = Armour;
data.Credits = Credits;
bf.Serialize (file, data);
file.Close ();
Debug.Log ("Save");
}
public void Load(){
Debug.Log (Application.persistentDataPath);
if(File.Exists(Application.persistentDataPath + "/GameInfo.dat")){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/GameInfo.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
Debug.Log ("Load");
file.Close();
Armour = data.Armour;
Credits = data.Credits;
}
}
[System.Serializable]
public class PlayerData{
public float Armour;
public int Credits;
}
}
Weird it works fine here. $$anonymous$$aybe the error is somewhere else.
I have discovered something new ,by putting some Debug.Logs into my update function xD. $$anonymous$$y save file for some reason does not take the new values of my variables but ins$$anonymous$$d uses the ones I had in the beginning of the scene,therefore when I load I get my base/original values!!!!!! :O soooo confused! .. Thank you all for your help by the way!
Answer by Voxel-Busters · Aug 08, 2015 at 03:04 AM
You can save all the trouble, by directly serializing MonoBehaviour using our plugin Runtime Serialization for Unity. Its not just another serialization plugin which works only on custom c# objects. But what makes it special is its capablity to serialize Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. As a matter of fact, you can even use it for Scene Serialization. For more info about supported list, please check this link.