- Home /
C# Serialization is working fine in Unity but not on Android
I'm aware, that there are multiple Questions about a similar topic out there already but still wasn't able to find a solution. I keep having the same problem, as already mentioned in the title I try saving data on android and in Unity everything is working as expected but as soon as i try on a real device it isn't saving anything (I tried 4 different devices). So today I tried again and even copied the code from an official tutorial (https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading) but I stil couldn't get it working. Here is my Script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
public class ScoreControll : MonoBehaviour {
public int Score = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject.Find("Score").GetComponent<Text>().text = "Score: " + Score;
}
void OnDisable()
{
Save();
}
void OnEnable()
{
Load();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/score.dat");
ScoreData data = new ScoreData();
data.Score = Score;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/score.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open((Application.persistentDataPath + "/score.dat"),FileMode.Open);
ScoreData data =(ScoreData) bf.Deserialize(file);
file.Close();
Score = data.Score;
}
}
}
[Serializable]
class ScoreData
{
public int Score;
}
I really hope for an easy answer.
Thanks in advance,
Pit Hüne
P.S. Im using Unity version 5.3.0f4 but I had the same issue in former versions aswell.
Hey,
I have the same issue. I am trying to create .dat file for saving username to external storage, but for some reason i could not get the file written to the location (app_domain/data/files). Thanks in advance
Daman
Answer by fledi1 · Jan 10, 2016 at 07:24 AM
I finally figured out the problem myself it was not a problem with saving, but with the methods. onDestroy , onDisable and similar ones, they are just not beeing called on android.
They are never called on android? So there is no callback on quit/kill the unity android app?
Answer by Yury-Habets · Jan 04, 2016 at 08:36 AM
What is the error you are getting?
Can you consider using PlayersPrefs http://docs.unity3d.com/ScriptReference/PlayerPrefs.html for saving data?
Thanks for your fast Answer, but I dont get any errors, thats why I think im doing something really basic wrong. I just tried the same thing with PlayerPrefs and again its all working fine in the emulator but as soon as I install and run it on a real android device it's not working anymore.
$$anonymous$$ Hüne
Your answer
Follow this Question
Related Questions
Unity WWW gzip 1 Answer
Unity Multiplayer Android Game 0 Answers
How can I read an XML file in Android ? I can't access it 1 Answer
Draw a line from object to indicate power 0 Answers
Multiple Cars not working 1 Answer