- Home /
My save function saves the wrong values!!
Thanks in advance. My save/load script is attached to game master object/prefab which travels between scenes , the load function works ,however my save function only saves the base values of my object.
e.g if i set my game master's prefab Armour and Credits value are set to 6 and 20 then the script only ever saves those values and never the updated values of Armour and Credits(I can see the updated values in the inspector when I play). What could be the root of this mystery?
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(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/GameInfo.dat");
Debug.Log (Application.persistentDataPath);
PlayerData data = new PlayerData ();
data.A = Armour;
Debug.Log (data.A);
Debug.Log (Armour);
data.C = Credits;
Debug.Log (data.C);
Debug.Log (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.A;
Debug.Log (data.A);
Debug.Log (Armour);
Credits = data.C;
Debug.Log (data.A);
Debug.Log (Credits);
}
}
[System.Serializable]
public class PlayerData{
public float A;
public int C;
}
}
How do you know your load function works if only the base values are saved?
Can you show us the output of your Debug.Log statements?
Answer by Garross · Aug 04, 2015 at 08:46 AM
Solved it , my save button was set to my game master script/prefab not the in game object holding the variables xD i'm soo stupid!
Your answer
Follow this Question
Related Questions
Error serializing a class to save / load 2 Answers
Save and load serialization problem 0 Answers
Can't get serialization Load/Save to work!C# 3 Answers
Coding help: How to use xml serialization 1 Answer
Unity Serializer scripting error 1 Answer