I am making a saving system for my app that uses JSON. However, it only works on PC. When I test it on Android, no file is created and no data is saved. I cannot seem to figure out what is causing it. I know the script is a bit long, but I am not familiar enough with writing into external files to know what is needed and what is not. In another file, a script has dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);
FileDataHandler script:
public class FileDataHandler
{
private string dataDirPath = "";
private string dataFileName = "";
public FileDataHandler(string dataDirPath, string dataFileName)
{
this.dataDirPath = dataDirPath;
this.dataFileName = dataFileName;
}
public GameData Load()
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
GameData loadedData = null;
if (File.Exists(fullPath))
{
try
{
string dataToLoad = "";
using (FileStream stream = new FileStream(fullPath, FileMode.Open))
{
using (StreamReader reader = new StreamReader(stream))
{
dataToLoad = reader.ReadToEnd();
}
}
loadedData = JsonUtility.FromJson<GameData>(dataToLoad);
}
catch (Exception e)
{
Debug.LogError("Error occured when trying to load data from file: " + fullPath + "
" + e);
}
}
return loadedData;
}
public void Save(GameData data)
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
try
{
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
string dataToStore = JsonUtility.ToJson(data, true);
using (FileStream stream = new FileStream(fullPath, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(dataToStore);
}
}
}
catch (Exception e)
{
Debug.LogError("Error occured when trying to save the file: " + fullPath + "
" + e);
}
}
}