- Home /
Why file creation on android is not working?
So I have a code that reads a high score and also saves it, it works fine while testing the game on the pc but the moment I send it to android it stops working, everything else is fine.
public void Save(int a)
{
score1 = a;
//Debug.Log (score1);
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + "/playerScore.dat");
PlayerData puntaje = new PlayerData ();
puntaje.score = score1;
bf.Serialize (file, puntaje);
file.Close();
}
public int Load()
{
if (File.Exists (Application.persistentDataPath + "/playerScore.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath+ "/playerScore.dat", FileMode.Open);
PlayerData puntaje = (PlayerData)bf.Deserialize(file);
file.Close();
score1 = puntaje.score;
}
return score1;
}
A much simpler way of saving a high score is by using PlayerPrefs, which will work on all platforms.
How did you go about coding it?
In the script that is counting the score, also have a high score variable. Then in your Update or Save function, you would have something like:
if(score > highScore)
{
highScore = score
PlayerPrefs.SetInt("High Score", highScore);
}
Then wherever you want to display or load the high score just use PlayerPrefs.GetInt("High Score")
According to the Unity reference, the files will be stored "in a public area" on the filesystem. This usually means SD card. Do you have the WRITE_EXTERNAL_STORAGE permission in your manifest?
compilign to try Jun Hin method, according to me I have the permission, in player settings-> other settings-> configuration I have the write access as External(sd card) unless there is something else im missing, which will make me facepalm until the end of time
Edit: didnt work either, still works perfectly on unity editor but not on android
Edit2: when i try to install the apk it tells me Allow this application to: Storage-$$anonymous$$odify/delet sd card contents, I'm really giving up
Answer by Tomer-Barkan · Apr 08, 2014 at 06:56 AM
Application.persistentDataPath
in android refers to the public (external) storage, which is the SD card. For this to work, you must specify that the application requires SD card write access. You can do this in the build menu, select android, click player settings, and in other settings there is a parameter called "Write Access", make sure it is set to "External".
Your other option would be to use Application.dataPath instead, and it will write it in the internal memory belonging to the application.
I have done around 10 different methods, persistentDataPath, player prefs, data path, write acces internal and external , text file ins$$anonymous$$d of binary, and combinations of all of the above, ALL of them works on my pc but not on the android phone ($$anonymous$$e or any1 else) .
Ok, In that case I'd start investigating other parts of my code. Add a debug log, check if the save/load methods are even called on the android device.