I have a saving/loading system (albeit from a youtube tutorial) that makes a .json file in the user’s “persistentDataPath”. On Windows, going to LocalRow > (company) > (game) shows that the .json is there, but when I go to data > com.(company).(game) > files on Android, the file isn’t there, even after playing the game and confirming that data should have been saved.
As far as I am aware, these are the relavant lines of code within my script with the “public class FileDataHandler”:
private string dataDirPath = "";
private string dataFileName = "";
public FileDataHandler(string dataDirPath, string dataFileName)
{
this.dataDirPath = dataDirPath;
this.dataFileName = dataFileName;
}
public void Save(GameData data)
{
string fullPath = Path.Combine(dataDirPath, dataFileName);
try
{
//create directory file
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
//serialize game data
string DataToStore = JsonUtility.ToJson(data, true);
//write the file
using (FileStream stream = new FileStream(fullPath, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(DataToStore);
}
}
}
catch (Exception e)
{
Debug.LogError("Error occurred when trying to save data to file: " + fullPath + "\n" + e);
}
}
In my other script, the “DataPersistenceManager” script, has these lines:
private GameData gameData;
private List<IDataPersistence> dataPersistenceObjects;
private FileDataHandler dataHandler;
void Start {
this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);
}
public void SaveGame()
{
//Pass data to other scripts
foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects)
{
dataPersistenceObj.SaveData(gameData);
}
//save data to a file
dataHandler.Save(gameData);
}
Does anyone know why this may be happening, or if I’m just not referencing something correctly?
Install logcat package and see if there is any error printed. I can’t easily spot anything wrong.
Also, how do you determine that it is not saved?
Also, how do you determine that it is not saved?
The .json file isn’t there, and restarting the game shows that nothing had saved.
OR
I know it should’ve saved as there’s a button in-game which saves the data when pressed (confirmed to work in Unity editor).
If there’s other parts of the scripts you wish to see, let me know.
But how do you determine that file isn’t there and not an issue with the reading code?
Note, that Android filesystem is case-sensitive, if by accident writing and reading has different case, it will work fine on Windows, but not on Android.
I’ve looked into the files and I saw that there was no file where there needed to be one.
“/storage/emulated/‘userid’/Android/data/‘packagename’/files” according to
Unity Documentation (or in my case the sdcard.
Sounds like a bug! Strap that debugger on, attach adb logcat
(as already noted above) and find out what’s going on at the moment you attempt to write the file. Any exceptions? Any confirmation your code is even running? Put in your own logging and find out, or attach a breakpoint.
This stuff doesn’t just magically not work. It’s time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...);
statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
I attempted to use logcat previously, and when I connected my device and ran the project no debug messages related to the project (as far as I was aware) showed up.
The project DOES have debug.log lines in the data saving & loading, but as I said those didn’t show up either.
Either get adb logcat
working (there’s millions of ways it can fail, all of them discussed online), or else you will have to purchase something like Lunar Console or else create your own UI code to print the logs to your screen.
You have to find out what is happening. We have no idea. It could literally be anything. That’s why we have logs and debugging. Really.
Is there a difference between adb logcat
and Android Logcat
or are they different names for the same package?
adb
is the android tool, logcat
is the command you send to it.
I assume Android Logcat
is just the Unity wrapper shell thingy on it to make it show in your console.
Put something in your code that prints out “WOOHOO!” and make sure you can find that in the logs. Don’t be afraid to crawl before you can walk… gotta prove these flaky toolchains out all the time.