- Home /
Question by
seanmor96 · Mar 04, 2021 at 04:49 PM ·
2dserializationloadingsavingbinaryformatter
Serialized Data Not Saving on Android Build
Hi, I am currently making a 2D android platformer and I am using the serialized data saving and loading system. For some reason everything works perfectly in the editor but when I build my game onto Android, nothing is being saved. The game runs perfectly without any errors but everything just does not save. I have done the necessary debugs to make sure all data files are being created in the right path location and everything seems to be fine. Even the data files show up in my phones storage. Here is the save system script:
public static class SaveSystem
{
public static void SavePlayerData(Player player)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Path.Combine(Application.persistentDataPath, "playerData.dat");
FileStream stream = new FileStream(path, FileMode.Create);
PlayerData playerData = new PlayerData(player);
formatter.Serialize(stream, playerData);
stream.Close();
}
public static PlayerData LoadPlayerData()
{
string path = Path.Combine(Application.persistentDataPath, "playerData.dat");
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
PlayerData playerData = formatter.Deserialize(stream) as PlayerData;
stream.Close();
return playerData;
}
else
{
Debug.LogError("Save file not found in" + path);
return null;
}
}
}
I call the save and load functions from that class in my game manager class:
public void SavePlayerData()
{
SaveSystem.SavePlayerData(player);
}
public void LoadPlayerData()
{
PlayerData playerData = SaveSystem.LoadPlayerData();
player.coins = playerData.totalCoins;
player.highscore = playerData.highscore;
}
I know this question has come up before but none of the solutions have helped me in any way so any help would be appreciated!
Comment