-1

I've got a file where I'm saving the highest score. In Unity Editor path is: path = Application.dataPath + "/Score.json"; And I don't know how to get path to this file in Android. Error has been deleted

Karol
  • 5
  • 1
  • 6
  • You save to use `Application.persistentDataPath` not `Application.dataPath`. You also have to check if the file or directory exist before attempting to save this. Regardless, you are saving game data as json and I think this is a duplicate. – Programmer Mar 16 '17 at 15:55

2 Answers2

2

Application.dataPath points to .apk file on Android as far as I know. I would suggest you using different paths on each platforms. This is what I prefer to save for saving some game stuff.

 public static string Path
        {
            get
            {
                switch (Application.platform)
                {
                    case RuntimePlatform.IPhonePlayer:
                        return Path.Combine(Application.persistentDataPath, "MyStuff");

                    case RuntimePlatform.Android:
                        return Path.Combine(Application.temporaryCachePath, "MyStuff");

                    default:
                        return Path.Combine(Directory.GetParent(Application.dataPath).FullName, "MyStuff");
                }
            }
        }
piotrmitrega
  • 308
  • 1
  • 6
1

On Android, Application.dataPath will point to the APK so you should avoid using it for save data since you wont be able to write to it. Instead, you want to use Application.persistentDataPath. As the linked documentation states:

When publishing on iOS and Android, persistentDataPath will point to a public directory on the device. Files in this location won't be erased with each update of the App.

Foggzie
  • 9,691
  • 1
  • 31
  • 48