Search Unity

//SOLVED JSON file can't be found in Application.persistentDataPath

Discussion in 'Scripting' started by DroidifyDevs, Sep 10, 2017.

  1. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hi,

    So I'm trying to use JSON to save my data instead of Binary Serialization which I've used before. It seems easier, but there's a problem: I can't find the JSON file, and no data gets loaded.

    Here's the code (some of it unrelated):
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. [System.Serializable]
    4. public class TileItems
    5. {
    6.     public Dictionary<Transform, Vector3> Objects = new Dictionary<Transform, Vector3>();
    7.     public string OverWriteThis;
    8. }
    9.  
    10. public class TilePopulatorV2 : MonoBehaviour
    11. {
    12.     #region "Variables"
    13.     public float MinXRange;
    14.     public float MaxXRange;
    15.     public float MinZRange;
    16.     public float MaxZRange;
    17.     public float TileWidth;
    18.     public TileItems tiles;
    19.     public JSONSaver SaveScript;
    20.     public string JSONString;
    21.     public bool SaveBool;
    22.     #endregion
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.         TileWidth = transform.GetComponent<Renderer>().bounds.extents.x;
    27.         LoadData();
    28.     }
    29.  
    30.     private void Update()
    31.     {
    32.         if(SaveBool)
    33.         {
    34.             SaveData();
    35.             SaveBool = false;
    36.         }
    37.     }
    38.  
    39.     public void SpawnObject(Transform CurrentObject)
    40.     {
    41.         TileWidth = transform.GetComponent<Renderer>().bounds.extents.x;
    42.         MinXRange = transform.position.x - (TileWidth);
    43.         MaxXRange = transform.position.x + (TileWidth);
    44.         MinZRange = transform.position.z - (TileWidth);
    45.         MaxZRange = transform.position.z + (TileWidth);
    46.         var spawnobj = Instantiate(CurrentObject, new Vector3(UnityEngine.Random.Range(MinXRange, MaxXRange), 0,
    47.                 UnityEngine.Random.Range(MinZRange, MaxZRange)), Quaternion.identity, transform);
    48.         spawnobj.localScale = new Vector3(0.2f, 0.2f, 0.2f);
    49.         tiles.Objects.Add(spawnobj.transform, spawnobj.position);
    50.     }
    51.  
    52.     public void SaveData()
    53.     {
    54.         tiles.OverWriteThis = "TESTING";
    55.         JSONString = JsonUtility.ToJson(tiles);
    56.         Debug.Log("Saved " + Application.persistentDataPath);
    57.     }
    58.  
    59.     public void LoadData()
    60.     {
    61.         tiles = JsonUtility.FromJson<TileItems>(JSONString);
    62.         Debug.Log("Loaded");
    63.     }
    64. }
    My Application.persistentDataPath is C:/Users/ahn20/AppData/LocalLow/Feline Interactive/Forest Explorer 1, but when I go there in the File Explorer, there's a Unity folder but it's full on Analytics files. A search for .json gives nothing.

    Why does the data seem to save, but doesn't load, and why isn't the file being created, or if it is, where is it? Is there something I'm missing in this code?

    Thank you!
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    You have the JSON string, but your not actually saving the data anywhere.
     
  3. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Ouch, the Unity doc is incomplete. I thought it would create the file automatically when calling ToJson.

    This does the trick perfectly:
    Code (CSharp):
    1.     public void SaveData()
    2.     {
    3.         JSONString = JsonUtility.ToJson(tiles);
    4.         File.WriteAllText(Application.persistentDataPath + "/MyGame.json", JSONString);
    5.         Debug.Log("Saved " + Application.persistentDataPath);
    6.     }
    7.  
    8.     public void LoadData()
    9.     {
    10.         tiles = JsonUtility.FromJson<TileItems>(File.ReadAllText(Application.persistentDataPath + "/MyGame.json"));
    11.         Debug.Log("Loaded");
    12.     }
    Thanks!
     
    CorteseSoftware and Reckope like this.
  4. CorteseSoftware

    CorteseSoftware

    Joined:
    Nov 26, 2021
    Posts:
    1
    Thank you for this!