- Home /
Save&load 100% works on pc, but not on android!
Hi guys, I Just code my first save and load script for my first android app. It works fine, 0 errors on pc, but when I build the game and run it on android nothing is saved after saving. Maybe is something wrong in the code? Or maybe is something about android permissions? in fact in the smartphone "app" area, I can see that my app has 0 permissions, maybe it cant write/read files. Any suggestions? Thanks :D I will paste my code! (c sharp)
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class SaveLoad : MonoBehaviour {
public static void Save(){
BinaryFormatter binaryFormatter = new BinaryFormatter ();
using (FileStream fs = new FileStream ("gamesave.bin", FileMode.Create, FileAccess.Write))
{
binaryFormatter.Serialize (fs, RecordCheck.recordvalue);
binaryFormatter.Serialize (fs, SoundController.muted);
Debug.Log ("SaveFileUpdated");
}
}
public static void Load()
{
if (!File.Exists ("gamesave.bin")) {
Debug.Log ("SaveFileNotFound");
return;
}
if (LoadStarter.alreadyloaded == 1) {
Debug.Log ("YouAlreadyLoadedTheSaveFile");
return;
}
else
{
BinaryFormatter binaryFormatter = new BinaryFormatter ();
using (FileStream fs = new FileStream ("gamesave.bin", FileMode.Open, FileAccess.Read)) {
RecordCheck.recordvalue = (int)binaryFormatter.Deserialize (fs);
SoundController.muted = (int)binaryFormatter.Deserialize (fs);
Debug.Log ("SaveFileLoaded");
}
}
}
}
I think you need save gamesave.bin with https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
Thats what I said about 7 hours ago, but he has ignored it
np. while you can just use normal + to concatenate their is a built in function for combining paths. eg..
public string fileName = "gamesave.bin";
private string filePath;
private void Start()
{
filePath = Path.Combine(Application.persistentDataPath, fileName);
}
Thanks, when at home I will try. It is my first experience in unity so I'm a nooby :) but what about app permissions? It has no permission when I check it
Answer by unit_nick · Oct 18, 2017 at 02:36 AM
I'm not sure but shouldn't you be using a Unity path in your file path? https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
Your answer
Follow this Question
Related Questions
im getting a serialization exception eror in my load method 0 Answers
Dinamic name for data save 0 Answers
Where to save player progress for playstore app? 1 Answer
Save and Load in the build 0 Answers