Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

here is a weird one - reading from JSON file gives value of zero on android but works in editor

Discussion in 'Scripting' started by lz7cjc, Feb 3, 2020.

  1. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    520
    Hi
    I am calculating a variable and writing to a JSON file. When I do this in the Unity Editor all the values come through as expected. But when I build the android app I get a value of zero. It reads the value correctly when I use dataPath, but it doesn't work when I use persistentDataPath

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         //build or test change path
    4.         //
    5.         //for app build
    6.         //      string json = File.ReadAllText(Application.persistentDataPath + "/saveFile.json");
    7.         //for testing
    8.           string json = File.ReadAllText(Application.dataPath + "/saveFile.json");
    9.  
    10.  
    11.         PlayerData loadedPlayerData = JsonUtility.FromJson<PlayerData>(json);
    12.         JeopardyCoefficent = loadedPlayerData.jeopardyCoefficient;
    13.         Debug.Log("JeopardyCoefficent pre in STATUS SCORE" + JeopardyCoefficent);
    my hacked code is pretty ugly so hopefully this is enough to go on!
    thanks
     
  2. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    520
    NB when i check the JSON file the value is 80 so it is not being read correctly
     
  3. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,637
    On Android most paths point to a directory inside apk file. The contents inside those directories cannot be read using file APIs, but you can read them using UnityWebRequest (those paths are jar:file:// URIs).
     
    diXime likes this.
  4. lz7cjc

    lz7cjc

    Joined:
    Sep 10, 2019
    Posts:
    520
    Fixed it
    Thanks
     
  5. isotian

    isotian

    Joined:
    Mar 8, 2015
    Posts:
    6
    Hey,
    How have you fixed the code so that you can read from persistentDataPath on Android?
    Can you write to persistentDataPath as well on Android or maybe other plarform?
    Happy new year
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,525
    You can read / write from / to the persistant data path just fine using the normal File IO methods. However this is pretty much the only folder on Android where this is possible. Keep in mind that the persistant data path is the equivalent of the application specific AppData folder on Windows. AFAIK persistantDataPath usually points to the AppData folder on windows. This folder will be empty unless your application puts something there. You can not ship information in this folder. You can only store information there and retrieve it later.

    If you want to ship a json file with your application which you can read and write on the device, the common solution is to include the "default" / initial file either as TextAsset or inside the StreamingAssets folder and extract / copy this file to the persistant folder at the first run (i.e. when the file does not exist yet). From that point on you can happily read and write your file in the persistant data path. Though as I said, you have to store something in that folder before you can read it ^^.

    When you use a TextAsset to ship your file you can directly setup a reference to your text file in the editor by making a public TextAsset field in your script and assign your text file to it in the inspector. In code you can directly read "textAsset.text" and you get the content of that text asset back. If you use the StreamingAssets folder you have to use a UnityWebRequest and use the partial URI that you get from the Application.streamingAssetsPath property.
     
    isotian likes this.