- Home /
Serializer deletes saved info on game restart
So I've been working on this game and there's a big issue when I go to run it on the phone ( Galaxy Note5) is that it doesn't keep any of the data if the game is force closed or reset for some reason. As well when I exit the game it doesn't fully close unless you close it from the program menu but that seems to reset the info as well. here's my script:
using UnityEngine; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class GameStatsSc : MonoBehaviour { public static GameStatsSc GameStats;
public int soapChipsObtained;
public int soapBarsObtained;
public float metersTraveled;
public bool hasRevive;
public int numberOfRevives;
public bool usedRevive;
public bool hasTwoHundred;
public int numberOfTwoHundreds;
public bool hasOneThousand;
public int numberOfOneThousands;
public bool hasBlackBody;
public bool hasBlackBodyEquipped;
public bool hasRedBody;
public bool hasRedBodyEquipped;
public bool hasGoldBody;
public bool hasGoldBodyEquipped;
public bool hasArmoredBody;
public bool hasArmoredBodyEquipped;
public bool hasFireBody;
public bool hasFireBodyEquipped;
void Awake () {
Debug.Log (Application.persistentDataPath);
if (GameStats == null) {
DontDestroyOnLoad (gameObject);
GameStats = this;
} else if (GameStats != this) {
Destroy (gameObject);
}
Load ();
}
void OnEnable(){
Load ();
}
void OnDisable(){
Save ();
}
public void Save(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/roundmanplayerInfo.dat");
PlayerData data = new PlayerData ();
data.soapChipsObtained = soapChipsObtained;
data.SoapBarsObtained = soapBarsObtained;
data.metersTraveled = metersTraveled;
data.hasRevive = hasRevive;
data.numberOfRevives = numberOfRevives;
data.hasTwoHundred = hasTwoHundred;
data.numberOfTwoHundreds = numberOfTwoHundreds;
data.hasOneThousand = hasOneThousand;
data.numberOfOneThousands = numberOfOneThousands;
data.hasBlackBody = hasBlackBody;
data.hasRedBody = hasRedBody;
data.hasGoldBody = hasGoldBody;
data.hasArmoredBody = hasArmoredBody;
data.hasFireBody = hasFireBody;
bf.Serialize (file, data);
file.Close ();
}
public void Load(){
if (File.Exists (Application.persistentDataPath + "/roundmanplayerInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open (Application.persistentDataPath + "/roundmanplayerInfo.dat",FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
soapChipsObtained = data.soapChipsObtained;
soapBarsObtained = data.SoapBarsObtained;
metersTraveled = data.metersTraveled;
hasRevive = data.hasRevive;
numberOfRevives = data.numberOfRevives;
hasTwoHundred = data.hasTwoHundred;
numberOfTwoHundreds = data.numberOfTwoHundreds;
hasOneThousand = data.hasOneThousand;
numberOfOneThousands = data.numberOfOneThousands;
hasBlackBody = data.hasBlackBody;
hasRedBody = data.hasRedBody;
hasGoldBody = data.hasGoldBody;
hasArmoredBody = data.hasArmoredBody;
hasFireBody = data.hasFireBody;
}
}
}
[Serializable] class PlayerData { public int soapChipsObtained; public int SoapBarsObtained; public float metersTraveled; public bool hasRevive; public int numberOfRevives; public bool hasTwoHundred; public int numberOfTwoHundreds; public bool hasOneThousand; public int numberOfOneThousands; public bool hasBlackBody; public bool hasRedBody; public bool hasGoldBody; public bool hasArmoredBody; public bool hasFireBody; }
I'm not sure what to do otherwise as it works with the computer Unity engine but not the phone. Please assist.
Answer by dsamuelson · Nov 15, 2015 at 10:17 AM
I notice that the problem seems unique to samsung devices. Don't know why but it should be working with anything but I can get it all to work on my other devices with no issue but any samsung device seems to just stop working with anything but player prefs.
Your answer
Follow this Question
Related Questions
Unity Not Loading 0 Answers
SerializationException: Type UnityEngine.UI.Button is not marked as Serializable. Solution 1 Answer
Save and load serialization problem 0 Answers
Save/load playerprefs 2 Answers
Saving/Load using menu C# 1 Answer