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

Json file not found after building

Discussion in 'Scripting' started by markahughes88, Aug 27, 2020.

  1. markahughes88

    markahughes88

    Joined:
    Aug 24, 2020
    Posts:
    19
    I'm using a JSON file to load data into the game. It works fine in the editor but when I build and run it cant find the json file.

    Code (CSharp):
    1.     private string m_JsonPath = "/Resources/PlayerData.json";
    2.  
    3.     // Update player data from PlayerData.json
    4.     public void LoadPlayerData()
    5.     {
    6.         using (StreamReader stream = new StreamReader(Application.dataPath + m_JsonPath))
    7.         {
    8.             m_Json = stream.ReadToEnd();
    9.             m_Player = JsonUtility.FromJson<Player>(m_Json);
    10.         }
    11.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    If you want a file to be included in your build verbatim, you need to put it in the StreamingAssets folder:

    https://docs.unity3d.com/Manual/StreamingAssets.html

    Unity takes everything else and flattens and compresses it into a big ball of unrecognizable Unity-soup.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Application.dataPath is different between Editor and a build, and your "Resources" folder is not readily accessible through the file system in a build. Use Streaming Assets for this - put your JSON in a Streaming Assets folder and you can access it the same in build as in editor.

    You could use Resources.Load if you treat the JSON file as a TextAsset.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Did you create a folder named Resources and place the json file there? If this is referring to a Resources folder inside your Assets folder, that won't work because the Assets folder doesn't actually exist in a build (go try to find it, you won't). As already mentioned, use StreamingAssets instead.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Unless you're really married to using all that non-Unity3D StreamReader chuffiness you can avoid all of this by dragging the JSON file into a public reference on the script that needs it:

    Code (csharp):
    1. public TextAsset TheJSONFile; // drag your JSON into this.
    and access it with

    Code (csharp):
    1. m_Json = TheJSONFile.text;
    Then no silliness with filesystems: Unity will just connect it all up and it will function identically in build or in editor.
     
  6. markahughes88

    markahughes88

    Joined:
    Aug 24, 2020
    Posts:
    19
    cheers for the help guys.

    So i attempted to set it up using the streaming assets documents first and placed the file in "Resources/Data/StreamingAssets/" director but that still failed to work. I'm not sure why?

    I then managed to get it working using Resources.Load instead. So that was great.

    However, I've now set up a save function to rewrite the file when needed. However, because I'm using Resources.Load and that doesn't exist at runtime i've ran into the problem of not being able to save. Again, it works fine in the editor as expected, but not after build.

    Code (CSharp):
    1.     private string m_JsonPath = "/Resources/PlayerData.json";
    2.     private string m_Json;
    3.     public Player m_Player = new Player();
    4.  
    5.     // Update player data from PlayerData.json
    6.     public void LoadPlayerData()
    7.     {
    8.         TextAsset json = Resources.Load<TextAsset>("PlayerData");
    9.         m_Json = json.ToString();
    10.         m_Player = JsonUtility.FromJson<Player>(m_Json);
    11.     }
    12.  
    13.     public void SavePlayerData(string type, string[] value)
    14.     {
    15.         if (type == "Username")
    16.             m_Player.Username = value[0];
    17.  
    18.         string saveData = JsonUtility.ToJson(m_Player);
    19.         File.WriteAllText(Application.dataPath + m_JsonPath, saveData);
    20.     }
     
  7. kaarloew

    kaarloew

    Joined:
    Nov 1, 2018
    Posts:
    360
    StarManta and markahughes88 like this.
  8. markahughes88

    markahughes88

    Joined:
    Aug 24, 2020
    Posts:
    19
  9. gaston595

    gaston595

    Joined:
    Apr 25, 2021
    Posts:
    1
    Hola, quero decirles gracias a todos, yo tuve el mismo problema y gracias a esto lo pude solucionar :)
     
    mariotabv likes this.
  10. StevenDeLaTorre

    StevenDeLaTorre

    Joined:
    Jan 4, 2019
    Posts:
    2
    This is such a smart simple answer and really had me going, "why didn't I think of that!"

    Works perfect! ... Sometimes you have so much else to do on your project that there is no point in losing whole days trying to make it work "the smart way". The text file can still be edited by a non-programmer and we don't need the end user to have access to the file anyway.
     
    Kurt-Dekker likes this.