Serialization Data not working on Android?

I’m upgrading my game to be able to save fastest completion time for each level using serialization. I’ve tested on the editor and on PC as standalone and it works fine. Running it on Android however, it doesn’t seem like it is writing the data thus leaving fastest time record empty.

Here’s the coding that deals with the saving and loading the best time:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public class PlayTimer : MonoBehaviour {

	public Text timerText;
	public bool gameEnd = false;
	public Text currentBestTime;
	public Text yourTime;

	private float startTime;
	public string finalTime;

	float t;

	float minutes;
	float seconds;
	float milliseconds;
	float totalMs;
	float totalBest;

	float bestMinutes;
	float bestSeconds;
	float bestMilliseconds;

	public int level;
	public float targetGold;
	public float targetSilver;

	public GameObject goldMedal;
	public GameObject silverMedal;
	public GameObject bronzeMedal;

	TimeData timer = new TimeData();

	void Start () {
		startTime = Time.time;
		Load ();
	// Update is called once per frame
	void Update () {

		if (gameEnd) {
			finalTime = timerText.text;
			yourTime.text = timerText.text;
			totalMs = ((minutes * 60) * 1000) + (seconds * 1000) + milliseconds;

			if (totalMs <= targetGold) {
				goldMedal.SetActive (true);
			} else if (totalMs <= targetSilver) {
				silverMedal.SetActive (true);
			} else {
				bronzeMedal.SetActive (true);

			if (totalMs < totalBest) {
				//Debug.Log (totalMs);
				//Debug.Log (totalBest);
				Save ();

		t = Time.time - startTime;

		minutes = ((int)t / 60);
		seconds = (t % 60);
		milliseconds = (t * 1000);

		milliseconds = milliseconds % 1000;

		timerText.text = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");

	public void Save(){
		//Debug.Log ("Saving...");
		BinaryFormatter bf = new BinaryFormatter ();
		FileStream stream = File.Open (Application.persistentDataPath + "/timer.sav", FileMode.OpenOrCreate);

		timer.minute[level] = minutes;
		timer.second[level] = seconds;
		timer.millisecond[level] = milliseconds;

		if (totalMs <= targetGold) {
			timer.medal [level] = 3;
		} else if (totalMs <= targetSilver) {
			timer.medal [level] = 2;
		} else {
			timer.medal [level] = 1;

		bf.Serialize (stream, timer);
		stream.Close (); //finalize the saving

	public void Load(){
		//Debug.Log ("Loading...");
		if (File.Exists (Application.persistentDataPath + "/timer.sav")) {
			BinaryFormatter bf = new BinaryFormatter ();
			FileStream stream = File.Open (Application.persistentDataPath + "/timer.sav", FileMode.Open, FileAccess.Read);

			timer = (TimeData)bf.Deserialize(stream);

			stream.Close ();

			bestMinutes = timer.minute[level];
			bestSeconds = timer.second[level];
			bestMilliseconds = timer.millisecond[level];

			totalBest = ((bestMinutes * 60) * 1000) + (bestSeconds * 1000) + bestMilliseconds;

			if (bestMilliseconds > 0.0) {
				SetBestTime (true);

		} else {
			SetBestTime (false);
			//Debug.LogError ("File does not exist.");

	public void SetBestTime(bool Set){
		if (Set) {
			currentBestTime.text = "Best Time:	" + bestMinutes.ToString("00") + ":" + bestSeconds.ToString("00") + ":" + bestMilliseconds.ToString("00");
		} else {
			currentBestTime.text = "Best Time:	--:--:--";

public class TimeData {

	public float[] minute;
	public float[] second;
	public float[] millisecond;
	public int[] medal;

	public TimeData() {
		minute = new float[50];
		second = new float[50];
		millisecond = new float[50];
		medal = new int[50];


I’ve been trying to find solution and fixes like use persistentDataPath, making sure that the Write Access is set to External, double checking the AdroidManifest.xml and still to no avail.

This is my first time doing serialization on mobile so I’m not sure what else I could be missing since the test on PC seems to be working fine. Is there something I’m missing? I need to have this feature working by end of the month.

Hey, I think I have a better answer for you then

I wrote an Android data save several months ago and I see your problem almost immediately. When it comes to Android, you are actually going to want to use something called Streaming Assets Path. Why? When trying to access an APK file, because it is an archive, it becomes difficult to access (for example, FileInfo does not support archives)

I recommend using Streaming Assets because it is fairly easy to access on Android and allows you to access things like custom level files, player data, etc.

Also, player prefs is extremly unsafe. While technically anything can be broken into, there are dozens of player prefs readers and makes your game a lot less safe. Give these a read.

This is a snippet from what I have in my own Save/Load system. (This being the load function)

if (Utility.OSManager.Instance.GetOSType() == Utility.OSManager.OSType.Android)
                        Debug.Log("OS TYPE is Android");

                        string item = "Levels/" + name + "." + extension;
                        string androidFilePath = System.IO.Path.Combine(Application.streamingAssetsPath, item);
                        WWW www = new WWW(androidFilePath);

                        while (www.isDone == false)


                        string realPath = Application.persistentDataPath + name;
                        System.IO.File.WriteAllBytes(realPath, www.bytes);

                        androidFilePath = realPath;

                        if (File.Exists(androidFilePath))
                            Debug.Log("File found!");
                            BinaryFormatter bf = new BinaryFormatter();

                            // 1. Construct a SurrogateSelector object
                            SurrogateSelector ss = new SurrogateSelector();
                            // 2. Add the ISerializationSurrogates to our new SurrogateSelector
                            AddSurrogates(ref ss);

                            // 3. Have the formatter use our surrogate selector
                            bf.SurrogateSelector = ss;

                            FileStream file = File.Open(androidFilePath, FileMode.Open);

                            SaveGame loadedGame = (SaveGame)bf.Deserialize(file);


                            return loadedGame;
                            return null;

Seems like no one could answer it. Anyhow, I’ve decided to switch to PlayerPref instead and was able to structure it to be able to save information for each levels as PlayerPref can only save one “file” for the whole project, I add an increment number onto the filename allowing the game to save specific data for specific level.

Hope this will help others looking into saving multiple information per level.