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

Need urgent help: Unable to read a file from a path as unable to find file path

Discussion in 'Scripting' started by shilpa_ramteke, Oct 5, 2020.

  1. shilpa_ramteke

    shilpa_ramteke

    Joined:
    Oct 5, 2020
    Posts:
    5
    I am working on Unity project solution where we need to read a json file but unable to do so since application is not able to read the file path.
    The structure is as follows -
    Assets -> Viewer.cs (This is the unity script)
    Assets -> ThirdPartySourceCode folder -> Contains below two files -
    SourceCode.cs
    Source.json

    In Viewer.cs when we can read a json file by moving the file to Assets -> Resources folder

    However in SouceCode.cs file, it is not getting the file path even if the file is inside ThirdPartySourceCode folder or moved to Assets -> Resources folder

    Tried many approaches like giving the path to below method as follows
    Approach 1
    string path = "Assets/Resources/Source.json";

    Approach 2
    string path = "ThirdPartySourceCode/Source.json";


    Code in SourceCode.cs
    using (var reader = new StreamReader(path)
    {
    string resource = reader.ReadToEnd();
    vendorMacs = JsonConvert.DeserializeObject<List<VendorMacs>>(resource);
    reader.Close();
    }

    Tried finding the path inside SourceCode.cs using Application.persistentDataPath but it gives "Unity: NullReferenceException: Object reference not set to an instance of an object" when called in SourceCode.cs where we actually want it.

    Whereas it shows path value properly when called in unity script - Viewer.cs. But we do not want to access it in unity script file. We want to access it in SourceCode.cs which is internal source code file nothing to do with Unity main script.

    Please help as its urgent.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    syscrusher likes this.
  3. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    I haven't time at the moment to mock this up locally, but here are some things to check:
    • The code in SourceCode.cs is constructing a StreamReader which is in System.IO (standard C# class). It's not part of Unity, so it understands the operating system path and doesn't implicitly know where to find the main Unity data folder. You would need to prepend the path down to your Resources folder.
    • Application.persistentDataPath isn't the constant you need; that's for storing app data, not loading resources.
    Take a look at the documentation for UnityEngine.Resources.Load:
    https://docs.unity3d.com/ScriptReference/Resources.Load.html
    and also the implementation guidelines for the Resources class:
    https://docs.unity3d.com/ScriptReference/Resources.html

    Hope this helps. :)

    UPDATE: @PraetorBlue posted while I was typing -- My method will work, and my comment about persistentDataPath is correct, but their suggestion for StreamingAssets is better than mine for Resources, so go that route. :) As a matter of fact, my second recommendation (to look at the doc for Resources class) was specifically because the Resources.Load approach isn't actually the recommended way to do this -- I linked that because it was closest to the existing code that you have in the third-party asset.
     
    Last edited: Oct 5, 2020
  4. shilpa_ramteke

    shilpa_ramteke

    Joined:
    Oct 5, 2020
    Posts:
    5
    Thank you so much for the prompt replies, I tried adding below code -
    string path = "StreamingAssets/Source.json";
    using (var reader = new StreamReader(Application.streamingAssetsPath + path)
    {
    }

    But got below error when run from Android device -
    Unity: DirectoryNotFoundException: Could not find a part of the path "/jar:file:/data/app/com.abcsapplication-1GUMdBUr==/base.apk!/assets/StreamingAssets/Source.json".


    The Source.json file is placed at location - > Assets -> StreamingAssets
    Please help me as it is urgent.

    Can it be because they are saying below -
    "It is not possible to access the StreamingAssets folder on WebGL and Android platforms. Android uses a compressed .apk file. These platforms return a URL. Use the UnityWebRequest class to access the Assets."

    Does anyone know how to use it ?

    Thanks in advance.
     
    Last edited: Oct 8, 2020
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722