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

Bug Android problem with Resources.Load

Discussion in 'Scripting' started by unity_A06CA2994C1C84E94021, May 1, 2023.

  1. unity_A06CA2994C1C84E94021

    unity_A06CA2994C1C84E94021

    Joined:
    Mar 25, 2023
    Posts:
    7
    I want to read my level information using json files. Each level has an equal json file , for example level_1.json.
    All of these files are located in the /Assets/Resources/Levels folder in unity.
    The problem is the Resource.Load works fine in unity editor on pc, but when I build an apk file for android, everything stopped working an the level information can not be loaded.

    Here is the code to load the level information
    Code (CSharp):
    1. var levelFile = $"Levels/level_{LevelNo + 1}";
    2. TextAsset  level= Resources.Load<TextAsset>(levelFile);
    3. if (level != null)
    4. {
    5. var currentLevel = JsonUtility.FromJson<Level>(level.text);
    6. //the rest of my code
    7. }
    I read multiple threads with the same problem, but none of them fix my problem.
     
    Last edited: May 1, 2023
  2. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    185
    Have you checked the Android Logcat for error messages? I do something similar (loading JSON files) on Android, and can confirm that it does work.
     
  3. unity_A06CA2994C1C84E94021

    unity_A06CA2994C1C84E94021

    Joined:
    Mar 25, 2023
    Posts:
    7
    Thanks,
    I check it and I found that it was because of a line code right before defining the levelFile variable. I commented it, and the problem has solved.