- Home /
Saving no longer works on iOS
I released a Unity (4.6) game for iOS last year, and saved the player's level progression using a binary formatter. Everything worked fine, but this past week I've been working on an update and am seeing a strange breaking of the saving system (which I have not touched).
When I build/run the project in Xcode, if the game is not yet on the device (whether iPad or iPhone), saving works properly. If I build it again, saving no longer works. If I run the game from the device, saving no longer works.
I haven't been able to determine if the problem is with my Xcode project, or with the game itself (because maybe something has changed in recent iOS updates?).
Here's some of the code to show how I handle things. These are all from a "ManagerLevels" class attached to a game object in the level selection screen (it isn't a child of anything so shouldn't be getting destroyed).
public static ManagerLevels levelControl;
public static bool [] completedLevels = new bool[35]; // 35 so can use 1-34
[Serializable]
class LevelData {
public bool [] completedLevels;
}
void Awake () { // Simple Singleton
if (levelControl == null) {
DontDestroyOnLoad(gameObject);
levelControl = this;
Load();
}
else if (levelControl != this) {
Destroy(gameObject);
}
}
public static void Load() {
if (File.Exists(Application.persistentDataPath + "/levelInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/levelInfo.dat", FileMode.Open);
LevelData data = (LevelData) bf.Deserialize(file);
file.Close();
completedLevels = data.completedLevels;
}
else {
// If no save data, set all completed level bools false (to be safe)
for (int i=1; i<(completedLevels.Length); i++) {
completedLevels[i] = false;
}
}
}
public static void Save() {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/levelInfo.dat");
LevelData data = new LevelData();
data.completedLevels = completedLevels;
bf.Serialize(file, data);
file.Close();
}
Okay, this is...interesting.
I added some OnGUI reporting boxes to the $$anonymous$$anagerLevels class to monitor the load/save behavior. When I push a new build from Xcode onto the device, the OnGUI reporting boxes are there on the screen (and saving works).
However, after stopping the game (through Xcode) and making sure it is closed on the device, when I start the game up on the device again, I'm not seeing any OnGUI boxes - it's as if the $$anonymous$$anagerLevels class isn't even running.
I'm trying to figure out how this might be happening...
Answer by Backward pieS · Jun 25, 2015 at 08:26 PM
Yes, I did solve it. I was missing the following line in the Awake method (before anything else inside):
Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
Worked fine after adding that.
Your answer