I recently upgraded our project from Unity 5.5.0p3 to Unity 2017.1.0f3 today. I noticed one action in our game was causing it to crash on Android. I isolated it down to saving some data to a file after doing that action (essentially visual data for what they have equipped). Once I commented out the saving it was fine and didn’t crash.
To test this a bit more, I created a function to see if saving anything crashes. So I used this function:
And nothing happened. There was no exception, and I didn’t see the actual file on the device. I hooked up the device to Android Studio and saw which file it was writing to, and “File doesnt exist, creating it” was also logged out. But no actual file was created.
Has anyone ever seen this happen before? Does anyone have any suggestions or ideas on what is going on?
Just an update to this. Turns out the above code is working when saving text. I came across this post on Stack Overflow which was the behaviour I was noticing:
After the above code ran, I was looking in Windows File Explorer to see if the file was created, I didn’t even think to look on the File Manager on the device. I checked File Manager on the device and saw the file was created. So that part of the issue is solved.
However…
There is still the crash issue that I am experiencing with our saving code that we use. I added extra log lines to see where it crashed:
[Serializable]
public class PlayerDisplayData
{
public string name;
public string gender;
public int skinColorId;
public int hairColorId;
public int hairStyleId;
public int favouriteColorId;
public int classDefId;
public int level;
public int weaponDefId;
public int armorDefId;
public int helmetDefId;
}
void SaveCharacterDisplayDataToDisk(PlayerDisplayData displayData)
{
try
{
BinaryFormatter bf = new BinaryFormatter();
Debug.Log("Trying to create file stream");
string filename = Application.persistentDataPath + "/display.dat";
FileStream file = File.Create(filename);
Debug.Log("Trying to serialize display data");
bf.Serialize(file, displayData);
Debug.Log("Trying to close file stream");
file.Close();
Debug.Log("DONE");
}
catch (Exception e)
{
Debug.LogError("Error saving display data to the device: " + e.ToString());
}
}
And here is the crash I am seeing in the logs in Android Studio (I replaced our app identifier with [GAME] in the logs):
Same problem. I’m using BinaryFormatter.Serialize to save game data. No problem in Unity 5.6.2f1. After upgraded to Unity 2017.1.0p5 it crash when calling BinaryFormatter.Serialize on Android device.