- Home /
My app generates a binary file but either fails to save data or load it || Unity for Android
Here's the problem: My script that produces a binary file works on pc, but fails on Android.
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameControl : MonoBehaviour {
public static GameControl gameControl;
[Header ("Store Variables")]
public float coins;
[Header("Time Variables")]
public DateTime currentDate;
public DateTime yesterdayDate;
[Header("Record Variables")]
public int bestScore;
[Header ("Inventory Variables")]
public int numberOfHeartBoosts;
public int numberOfTimeBoosts;
public int numberOfLockBoosts;
[Header("Settings Variables")]
public bool musicOn;
public float musicPercentage;
public bool sfxOn;
public float sfxPercentage;
public bool isTutorialActive;
[Header("Design Variables")]
public float buttonFontColorR = 1f;
public float buttonFontColorG = 0.5882353f;
public float buttonFontColorB = 0f;
public float buttonInnerBoxColorR = 0f;
public float buttonOuterBoxColorR = 1f;
public float buttonInnerBoxColorG = 0f;
public float buttonOuterBoxColorG = 0.5882353f;
public float buttonInnerBoxColorB = 0f;
public float buttonOuterBoxColorB = 0f;
public float themeColorR = 1f;
public float themeColorG = 1f;
public float themeColorB = 1f;
public float backgroundColorR = 1f;
public float backgroundColorG = 0.5882353f;
public float backgroundColorB = 0f;
[Header("Store Button Unlockables")]
public bool isButtonOnePurchased;
public bool isButtonTwoPurchased;
public bool isButtonThreePurchased;
public bool isButtonFourPurchased;
public bool isButtonFivePurchased;
public bool isButtonSixPurchased;
public bool isButtonSevenPurchased;
public bool isButtonEightPurchased;
public bool isButtonNinePurchased;
[Header("Store Theme Unlockables")]
public bool isThemeOnePurchased;
public bool isThemeTwoPurchased;
public bool isThemeThreePurchased;
public bool isThemeFourPurchased;
public bool isThemeFivePurchased;
public bool isThemeSixPurchased;
public bool isThemeSevenPurchased;
public bool isThemeEightPurchased;
public bool isThemeNinePurchased;
[Header("Store Background Unlockables")]
public bool isBackgroundOnePurchased;
public bool isBackgroundTwoPurchased;
public bool isBackgroundThreePurchased;
public bool isBackgroundFourPurchased;
public bool isBackgroundFivePurchased;
public bool isBackgroundSixPurchased;
public bool isBackgroundSevenPurchased;
public bool isBackgroundEightPurchased;
public bool isBackgroundNinePurchased;
void OnDisable(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.eut");
PlayerData data = new PlayerData();
data.currentDate = currentDate;
data.yesterdayDate = yesterdayDate;
data.coins = coins;
data.bestScore = bestScore;
data.numberOfHeartBoosts = numberOfHeartBoosts;
data.numberOfTimeBoosts = numberOfTimeBoosts;
data.numberOfLockBoosts = numberOfLockBoosts;
data.musicOn = musicOn;
data.musicPercentage = musicPercentage;
data.sfxOn = sfxOn;
data.sfxPercentage = sfxPercentage;
data.isTutorialActive = isTutorialActive;
data.isButtonOnePurchased = isButtonOnePurchased;
data.isButtonTwoPurchased = isButtonTwoPurchased;
data.isButtonThreePurchased = isButtonThreePurchased;
data.isButtonFourPurchased = isButtonFourPurchased;
data.isButtonFivePurchased = isButtonFivePurchased;
data.isButtonSixPurchased = isButtonSixPurchased;
data.isButtonSevenPurchased = isButtonSevenPurchased;
data.isButtonEightPurchased = isButtonEightPurchased;
data.isButtonNinePurchased = isButtonNinePurchased;
data.buttonFontColorR = buttonFontColorR;
data.buttonFontColorG = buttonFontColorG;
data.buttonFontColorB = buttonFontColorB;
data.buttonInnerBoxColorR = buttonInnerBoxColorR;
data.buttonInnerBoxColorG = buttonInnerBoxColorG;
data.buttonInnerBoxColorB = buttonInnerBoxColorB;
data.buttonOuterBoxColorR = buttonOuterBoxColorR;
data.buttonOuterBoxColorG = buttonOuterBoxColorG;
data.buttonOuterBoxColorB = buttonOuterBoxColorB;
data.isThemeOnePurchased = isThemeOnePurchased;
data.isThemeTwoPurchased = isThemeTwoPurchased;
data.isThemeThreePurchased = isThemeThreePurchased;
data.isThemeFourPurchased = isThemeFourPurchased;
data.isThemeFivePurchased = isThemeFivePurchased;
data.isThemeSixPurchased = isThemeSixPurchased;
data.isThemeSevenPurchased = isThemeSevenPurchased;
data.isThemeEightPurchased = isThemeEightPurchased;
data.isThemeNinePurchased = isThemeNinePurchased;
data.themeColorR = themeColorR;
data.themeColorG = themeColorG;
data.themeColorB = themeColorB;
data.isBackgroundOnePurchased = isBackgroundOnePurchased;
data.isBackgroundTwoPurchased = isBackgroundTwoPurchased;
data.isBackgroundThreePurchased = isBackgroundThreePurchased;
data.isBackgroundFourPurchased = isBackgroundFourPurchased;
data.isBackgroundFivePurchased = isBackgroundFivePurchased;
data.isBackgroundSixPurchased = isBackgroundSixPurchased;
data.isBackgroundSevenPurchased = isBackgroundSevenPurchased;
data.isBackgroundEightPurchased = isBackgroundEightPurchased;
data.isBackgroundNinePurchased = isBackgroundNinePurchased;
data.backgroundColorR = backgroundColorR;
data.backgroundColorG = backgroundColorG;
data.backgroundColorB = backgroundColorB;
bf.Serialize(file, data);
file.Close();
}
void OnEnable(){
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.eut", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close();
currentDate = data.currentDate;
yesterdayDate = data.yesterdayDate;
coins = data.coins;
bestScore = data.bestScore;
numberOfHeartBoosts = data.numberOfHeartBoosts;
numberOfTimeBoosts = data.numberOfTimeBoosts;
numberOfLockBoosts = data.numberOfLockBoosts;
musicOn = data.musicOn;
musicPercentage = data.musicPercentage;
sfxOn = data.sfxOn;
sfxPercentage = data.sfxPercentage;
isTutorialActive = data.isTutorialActive;
isButtonOnePurchased = data.isButtonOnePurchased;
isButtonTwoPurchased = data.isButtonTwoPurchased;
isButtonThreePurchased = data.isButtonThreePurchased;
isButtonFourPurchased = data.isButtonFourPurchased;
isButtonFivePurchased = data.isButtonFivePurchased;
isButtonSixPurchased = data.isButtonSixPurchased;
isButtonSevenPurchased = data.isButtonSevenPurchased;
isButtonEightPurchased = data.isButtonEightPurchased;
isButtonNinePurchased = data.isButtonNinePurchased;
buttonFontColorR = data.buttonFontColorR;
buttonInnerBoxColorR = data.buttonInnerBoxColorR;
buttonOuterBoxColorR = data.buttonOuterBoxColorR;
buttonFontColorG = data.buttonFontColorG;
buttonInnerBoxColorG = data.buttonInnerBoxColorG;
buttonOuterBoxColorG = data.buttonOuterBoxColorG;
buttonFontColorB = data.buttonFontColorB;
buttonInnerBoxColorB = data.buttonInnerBoxColorB;
buttonOuterBoxColorB = data.buttonOuterBoxColorB;
isThemeOnePurchased = data.isThemeOnePurchased;
isThemeTwoPurchased = data.isThemeTwoPurchased;
isThemeThreePurchased = data.isThemeThreePurchased;
isThemeFourPurchased = data.isThemeFourPurchased;
isThemeFivePurchased = data.isThemeFivePurchased;
isThemeSixPurchased = data.isThemeSixPurchased;
isThemeSevenPurchased = data.isThemeSevenPurchased;
isThemeEightPurchased = data.isThemeEightPurchased;
isThemeNinePurchased = data.isThemeNinePurchased;
themeColorR = data.themeColorR;
themeColorG = data.themeColorG;
themeColorB = data.themeColorB;
isBackgroundOnePurchased = data.isBackgroundOnePurchased;
isBackgroundTwoPurchased = data.isBackgroundTwoPurchased;
isBackgroundThreePurchased = data.isBackgroundThreePurchased;
isBackgroundFourPurchased = data.isBackgroundFourPurchased;
isBackgroundFivePurchased = data.isBackgroundFivePurchased;
isBackgroundSixPurchased = data.isBackgroundSixPurchased;
isBackgroundSevenPurchased = data.isBackgroundSevenPurchased;
isBackgroundEightPurchased = data.isBackgroundEightPurchased;
isBackgroundNinePurchased = data.isBackgroundNinePurchased;
backgroundColorR = data.backgroundColorR;
backgroundColorG = data.backgroundColorG;
backgroundColorB = data.backgroundColorB;
}
void Awake(){
if (gameControl == null){
DontDestroyOnLoad(gameObject);
gameControl = this;
}
else if (gameControl != this){
Destroy(gameObject);
}
}
}
[Serializable]
class PlayerData{
public DateTime currentDate;
public DateTime yesterdayDate;
public float coins;
public int bestScore;
public int numberOfHeartBoosts;
public int numberOfTimeBoosts;
public int numberOfLockBoosts;
public bool musicOn;
public float musicPercentage;
public bool sfxOn;
public float sfxPercentage;
public bool isTutorialActive;
public bool isButtonOnePurchased;
public bool isButtonTwoPurchased;
public bool isButtonThreePurchased;
public bool isButtonFourPurchased;
public bool isButtonFivePurchased;
public bool isButtonSixPurchased;
public bool isButtonSevenPurchased;
public bool isButtonEightPurchased;
public bool isButtonNinePurchased;
public float buttonFontColorR;
public float buttonFontColorG;
public float buttonFontColorB;
public float buttonInnerBoxColorR;
public float buttonOuterBoxColorR;
public float buttonInnerBoxColorG;
public float buttonOuterBoxColorG;
public float buttonInnerBoxColorB;
public float buttonOuterBoxColorB;
public bool isThemeOnePurchased;
public bool isThemeTwoPurchased;
public bool isThemeThreePurchased;
public bool isThemeFourPurchased;
public bool isThemeFivePurchased;
public bool isThemeSixPurchased;
public bool isThemeSevenPurchased;
public bool isThemeEightPurchased;
public bool isThemeNinePurchased;
public float themeColorR;
public float themeColorG;
public float themeColorB;
public bool isBackgroundOnePurchased;
public bool isBackgroundTwoPurchased;
public bool isBackgroundThreePurchased;
public bool isBackgroundFourPurchased;
public bool isBackgroundFivePurchased;
public bool isBackgroundSixPurchased;
public bool isBackgroundSevenPurchased;
public bool isBackgroundEightPurchased;
public bool isBackgroundNinePurchased;
public float backgroundColorR;
public float backgroundColorG;
public float backgroundColorB;
}
I've been stuck for days figuring out what is wrong with my script. I keep digging old threads and experimenting on my code, but nothing works. Forgive the way I code, I'm not really a programmer but the deadline is near, I'm working as fast as I could. Thanks in advance!
Answer by Bunny83 · Oct 09, 2018 at 10:19 PM
You shouldn't do this:
Application.persistentDataPath + "/PlayerData.eut"
You can't be sure how the persistentDataPath ends. If it ends with a slash you would have a double slash. You should always use System.IO.Path.Combine to concat a path with a filename
Path.Combine(Application.persistentDataPath, "PlayerData.eut")
I can't say for sure that this is the issue. There could be other things wrong. You have to debug this on your end. Check the logcat of your device.
Keep in mind that when an application is moved to the background, OnDisabled will not be called. You may want to use OnApplicationFocus or something similar. Once an app is in the background the app could be terminated at any time. Saving when loosing focus should only be a safety mechanism. It's better to save in between as long as the application still runs.
thanks for the fix! Its either the path goes to the wrong direction or it fails to save because of the case when OnDisable doesn't get called. I definitely learned stuff today, thanks again!
Your answer
Follow this Question
Related Questions
JSON not saving game object list 0 Answers
Save and (later) read scriptable objects 1 Answer