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;
}
}