Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Since Unity 2019.4.15, Resources.Load calls all fail until I rebuild

Discussion in 'Addressables' started by dgoyette, Nov 22, 2020.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    I updated from 2019.4.14 to 2019.4.15 this week. I've been finding that occasionally after opening Unity and entering Play mode, every call to Resources.Load that I make returns null. The call I'm making is simply
    Resources.Load<GameObject>(prefabResourcePath);
    , with various paths being used.

    This is despite all of the same expected content in Resources appearing the Addressable Groups window:

    upload_2020-11-22_11-40-1.png

    The "fix" so far as been to Build -> New Build -> Default Build Script. Upon doing that, it resolves the issue.

    Any idea if something in 2019.4.15 may have introduced an issue? Note that on updating to 2019.4.15, I also removed my Library folder to let it rebuild the projects contents, so that could be related. I'm using Addressables 1.16.10.
     
    themorfeus likes this.
  2. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    I'm having a simillar issue in 2020.1.14f1 - however in my case only a script recompile (i.e. chaning a script and making unity recompile) is required for it to start working.
    I can't seem to reproduce it consistently in an empty project.

    Are you using any plugins like Odin, Zenject, etc.?
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    I'm using a fair number of 3rd party assets, but none of those have changes in several months. I'm not using Odin or Zenject, though. It's not clear to me whether this is due to the version bump, or whether cleaning out my Library folder causes some dormant issue to surface.
     
  4. Pronetizen

    Pronetizen

    Joined:
    Nov 11, 2018
    Posts:
    11
    I have the same issue in my project on specific machine I have.

    2019.4.14f1 (mac machine A: this is a new machine I set up today.) - not working.
    2019.4.14f1 (mac machine B) - working and always worked.
    2019.4.14f1 (windows machine C) - working and always worked.

    As themorfeus said, only after script recompile, it started working on machine A.
    The problem always reoccurs after restarting Unity.

    Not reproduced in empty project
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    This has been gradually not been happening for me anymore. I don't think I've seen it in a few days now. After initially upgrading and deleting Library, it was happening every other time I reopened Unity.

    So, I still don't know why this was happening. I hate to think it had things it had to just work out, but it feels like that's what happened.
     
  6. Pronetizen

    Pronetizen

    Joined:
    Nov 11, 2018
    Posts:
    11
    Thank you for your update, dgoyette.

    The same issue detected in mac machine B after switching build target from iOS to Android. :(
    Even if I switch back to iOS build target, the issue persists. Haha...
     
  7. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    164
    I got the same issue in unity 2019.4.15

    I got easier workaround... Just select the problematic assets in Project window, right-click and reimport. After that everyhing is working

    Of course, if I quit Unity and try again, everything fails. Reimporting assets fixes the problem just for current session of unity
     
    ArthurClark and Eater_Games like this.
  8. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    709
    Same bug, in 2019.4.15

    great
     
  9. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    I think I probably just had a couple of lucky days in a row. Since my last post, I've had this happen once or two per day. Still definitely a problem.
     
  10. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    164
    Still happens in v2019.4.16
     
  11. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    Issue still prevails in 2020.1.16f1, but I haven't have launched the project since my last post in this thread. Upgrading unity did not help

    EDIT: I have found, however, that the issue seems to only happens if any project script is attached to the prefab i am trying to load. If it has no scripts other than unity's built in ones attached, it loads fine.

    I am using assembly definitions.
     
    Last edited: Dec 5, 2020
  12. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    Could any of the people that have this problem list the plugins they're using? This might help in narrowing down the issue, since it seems to not happen in blank projects.

    Mine are:
    • Extenject
    • Odin
    • HDRP
    • Input System
    • ProBuilder
    • ProGrids
    • Shader Graph
    • TextMeshPro
    • Searcher
    • Settings Manager
    • Custom NUnit
    • JetBrains Rider Editor
     
  13. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192

    {
    "dependencies": {
    "com.unity.2d.sprite": "1.0.0",
    "com.unity.addressables": "1.16.10",
    "com.unity.animation.rigging": "0.2.7-preview",
    "com.unity.burst": "1.4.1",
    "com.unity.cinemachine": "2.6.3",
    "com.unity.cloud.userreporting": "0.2.4-preview",
    "com.unity.collections": "0.9.0-preview.6",
    "com.unity.formats.fbx": "3.2.1-preview.2",
    "com.unity.jobs": "0.2.10-preview.13",
    "com.unity.mathematics": "1.2.1",
    "com.unity.nuget.newtonsoft-json": "2.0.0",
    "com.unity.probuilder": "4.4.0",
    "com.unity.progrids": "3.0.3-preview.6",
    "com.unity.render-pipelines.core": "7.4.1",
    "com.unity.render-pipelines.high-definition": "file:../LocalPackages/com.unity.render-pipelines.high-definition@7.5.1",
    "com.unity.render-pipelines.high-definition-config": "7.4.1",
    "com.unity.test-framework": "1.1.18",
    "com.unity.textmeshpro": "2.1.3",
    "com.unity.ugui": "1.0.0",
    "com.unity.vectorgraphics": "2.0.0-preview.13",
    "com.unity.visualeffectgraph": "7.4.1",
    "com.unity.modules.ai": "1.0.0",
    "com.unity.modules.androidjni": "1.0.0",
    "com.unity.modules.animation": "1.0.0",
    "com.unity.modules.assetbundle": "1.0.0",
    "com.unity.modules.audio": "1.0.0",
    "com.unity.modules.cloth": "1.0.0",
    "com.unity.modules.director": "1.0.0",
    "com.unity.modules.imageconversion": "1.0.0",
    "com.unity.modules.imgui": "1.0.0",
    "com.unity.modules.jsonserialize": "1.0.0",
    "com.unity.modules.particlesystem": "1.0.0",
    "com.unity.modules.physics": "1.0.0",
    "com.unity.modules.physics2d": "1.0.0",
    "com.unity.modules.screencapture": "1.0.0",
    "com.unity.modules.terrain": "1.0.0",
    "com.unity.modules.terrainphysics": "1.0.0",
    "com.unity.modules.tilemap": "1.0.0",
    "com.unity.modules.ui": "1.0.0",
    "com.unity.modules.uielements": "1.0.0",
    "com.unity.modules.umbra": "1.0.0",
    "com.unity.modules.unityanalytics": "1.0.0",
    "com.unity.modules.unitywebrequest": "1.0.0",
    "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    "com.unity.modules.unitywebrequestaudio": "1.0.0",
    "com.unity.modules.unitywebrequesttexture": "1.0.0",
    "com.unity.modules.unitywebrequestwww": "1.0.0",
    "com.unity.modules.vehicles": "1.0.0",
    "com.unity.modules.video": "1.0.0",
    "com.unity.modules.vr": "1.0.0",
    "com.unity.modules.wind": "1.0.0",
    "com.unity.modules.xr": "1.0.0"
    }
    }
     
  14. Pronetizen

    Pronetizen

    Joined:
    Nov 11, 2018
    Posts:
    11
    Some progress on this...

    I found Odin Inspector plugin causing the issue in my project. (themorfeus noted this earlier, thanks!)
    By removing this plugin, the problem seems to be solved at least for now.

    I will report here if the issue came back without the plugin.
     
  15. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    Any official word on whether this is a known issue? This isn't something I can easily reproduce, but I still deal with this once or twice a day, most typically after reopening Unity after a crash. This is still occurring in 2019.4.17
     
    themorfeus likes this.
  16. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    As a workaround I've written a script to refresh a single script asset on project launch, which fixes the issue for me. Chug this into an Editor folder, and it should work automatically, as long as you have at least a single script file in the project.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [InitializeOnLoad]
    5. public class ResourcesBugFixer
    6. {
    7.     static string ReloadedAssetsStateName = "ResourcesBugFixer.ReloadedAssets";
    8.  
    9.     static ResourcesBugFixer()
    10.     {
    11.         EditorApplication.delayCall += ReloadAssets;
    12.     }
    13.      
    14.     static void ReloadAssets()
    15.     {
    16.         if (!SessionState.GetBool(ReloadedAssetsStateName, false ))
    17.         {
    18.             SessionState.SetBool(ReloadedAssetsStateName, true);              
    19.             var guid = AssetDatabase.FindAssets("t:Script")[0];
    20.             AssetDatabase.ImportAsset(AssetDatabase.GUIDToAssetPath(guid));
    21.             Debug.LogFormat("Refreshed asset database");
    22.         }
    23.     }
    24. }
     
    awlok and Pronetizen like this.
  17. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    That's interesting. It does seem that reimporting a single script resolves the issue when it occurs. I just picked a random script, right-click -> Reimport.

    So, that's a couple of workarounds, for now. Reimporting a script definitely takes less time, and is more convenient than rebuilding Addressables each time this happens. Good find.
     
    themorfeus likes this.
  18. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    Actually, reimporting ANY asset will fix the issue, so my money's on something wrong with the asset database. I chose a script for the 'fix' script because scripts are usually very small in size, so a reimport of them is quick as well (i'd even go as far as saying unnoticeable in this case - during project launch).

    So at least with the script i had posted, my workflow has gone back to normal and i don't need to worry about it anymore. However if anyone experiences this issue at any other time than the project launch, it probably won't help.

    Would be nice if someone @ Unity could poke around, maybe they can find out something in the C++ source that we do not have access to.

    As a side-note, the issue still persists in a new, fresh project with Zenject and Odin imported in Unity 2020.2.1f1
     
  19. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    I would also suggest moving this thread into a more general bug forum, since this is not exclusive to addressables - in fact, the single project i do use addressables in is free of this issue. The project in question also uses Odin and Zenject.
     
    TreyK-47 likes this.
  20. masterton

    masterton

    Joined:
    Dec 11, 2012
    Posts:
    41
    Fix (for me anyway) was to reimport all assets. Now it can find the files via Resources.Load (for contest, I'm loading some icons for an editor window).
     
  21. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    The question is whether that's a long-term fix. It's been mentioned in this thread that reimporting any single asset will "clear" the error temporarily, until it happens again. So, it's not obvious whether your Reimport All just fixed it for now, similar to how a single file reimport would.
     
  22. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    164
    It solves the issue for the current Unity session only. When you restart you have to reimport again
     
  23. Jamez0r

    Jamez0r

    Joined:
    Jul 29, 2019
    Posts:
    205
    I also have this exact issue. I'm not using Zenject. Pretty annoying.
     
  24. AdrianLee76

    AdrianLee76

    Joined:
    Jan 20, 2021
    Posts:
    1
    upload_2021-1-20_11-20-11.png
    Unity 2019.4.1 version is no problem, but after Unity 2019.4.1 version there was a problem.
    This seems to be happening when Unity frequently adds new features.

    Resources.Load randomly returns null on existing files
    1. Problem case
    Asset Pipeline : Version 2
    2. Success case

    Asset Pipeline : Version 1 (deprecated)

    Discovered after being put into work on recovering a problem project. (in Korea)
     
  25. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    Yes, i've pasted a script above that does this automatically each session (and, it only reimports a single script file, which is enough for the fix to work)
     
  26. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,192
    One tiny optimization (and it seems a bit silly to be optimizing a workaround) would be to choose some asset that specifically isn't a script. One downside to reimporting a script is it kicks off a recompile/reload of scripts, which can take several seconds depending on the project. I tend to reimport an image, or a text file, since that's pretty much instant.
     
  27. themorfeus

    themorfeus

    Joined:
    Jun 14, 2013
    Posts:
    10
    I've specifically chosen scripts, since in the cases i've seen an image reload might actually take quite a bit longer, especially if you use things such as psb - which can sometimes take well above 10 minutes to reload, while a recompilation of quite a large codebase usually takes no more than 30 seconds on my machines, which, when accounted into the project startup time, is negligible.

    But i guess that should be decided on a per-project basis.
     
    dgoyette likes this.
  28. yusufdemir2012

    yusufdemir2012

    Joined:
    Dec 21, 2013
    Posts:
    49
    I have the same issue in my Unity project. The problem is occurring when project is opening, if I modify any script any save it, the problem is solved.

    Unity version : 2019.4.14f1
    And i also use Odin Inspector.
     
    Last edited: Mar 16, 2021
    Eater_Games likes this.
  29. KaraShiki

    KaraShiki

    Joined:
    Oct 8, 2019
    Posts:
    1
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.         UnityEditor.AssetDatabase.Refresh();
    3. #endif
    Add it on Play,Can solve the problem.
    But this is not a conventional approach.

    Unity version : 2020.1.1f1c1
    And i also use Odin Inspector.
     
  30. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,810
    Howdy all! I'm going to ping the team for an update on this one.
     
    MKrohn and themorfeus like this.
  31. MKrohn

    MKrohn

    Joined:
    Apr 5, 2018
    Posts:
    23
    I have the same issue (also using Odin Inspector), but with me it also seems to sometimes (rarely) happen in builds. Got a number of user/tester reports where they couldn't start the game because of NullReferenceExceptions from Resources.Load(..).

    Edit: Found another error in the user reports, so not sure if the NullRef is indeed part of this thread's problem. I'll keep an eye on it.
     
    Last edited: Apr 2, 2021
  32. TreyK-47

    TreyK-47

    Unity Technologies

    Joined:
    Oct 22, 2019
    Posts:
    1,810
    Connected with the team. This doesn't appear to be an Addressables issue. This looks like an Asset Importer or AssetDatabase problem.
     
  33. wolfgangmami

    wolfgangmami

    Joined:
    Jul 23, 2018
    Posts:
    11
    2020.3.4f1 Here. Any update ? I almost throw my pc out. What are the other options or alternatives ? Interestingly enough, resource.load is working an image when I need it. But in this script. It is partially working. For example: 1 mixer is loading and slider script is attaching. Others is not loading and unity giving me "exposed parameter doesn't exist" error.
    Code (CSharp):
    1. for (int i = 0; i < _sound_sliders.Length; i++)
    2.         {
    3.             string a = Path.GetFileNameWithoutExtension(Application.dataPath + "/Resources/Sounds/" + Universal_Scripts._fake_enums[i]);
    4.             _mixer[i] = Resources.Load<AudioMixer>("Sounds/" + a);
    5. }
     
    Last edited: Apr 19, 2021
  34. unity_wBBMYxlAWgCmgw

    unity_wBBMYxlAWgCmgw

    Joined:
    May 11, 2021
    Posts:
    1
    Hi, do you have any update on this?
    We are experiencing the same issue after migrating from 2018 LTS to 2019 LTS.
     
  35. KrcKung

    KrcKung

    Joined:
    Jul 18, 2017
    Posts:
    56
    Hi, we seem to hav similar issue happening on 2020.3.11f1 as well. When we make build with project that update from 2018.4.x to 2020.3.11f1, it has this Resources.Load return null issue happening in the builds when we built with Jenkins but such issue gone when we manually.
     
  36. MightyCoconutJP

    MightyCoconutJP

    Joined:
    Aug 4, 2022
    Posts:
    2
    Hi, we are having the same issue as @KrcKung. Specifically, we see the issue of Resources.Load return null mostly in builds from Jenkins, but it's intermittent. 99% of our Local/Manual builds meanwhile have not run into the issue.
     
  37. kayke

    kayke

    Joined:
    Oct 21, 2015
    Posts:
    42
    Seeing the same issue with builds from 2020.3.4f1
     
  38. MightyCoconutJP

    MightyCoconutJP

    Joined:
    Aug 4, 2022
    Posts:
    2
    Solution for us. Added the assets to the ProjectSettings Preloaded Assets and Resources.Load & LoadAll worked after that.
     
  39. hoonsubin

    hoonsubin

    Joined:
    Dec 7, 2017
    Posts:
    3
    Last edited: Jan 2, 2023
  40. ArminRigo

    ArminRigo

    Joined:
    May 19, 2017
    Posts:
    19
    I've got the same issue of Resources.Load<SteamVR_Settings> returning null, for me in the editor. It shows up in the SteamVR plugin and the traceback shows it is when called from a [UnityEditor.Callbacks.DidReloadScripts] callback. So my guess is that this callback is invoked too early, at a point where the resource cannot be loaded yet. Or maybe some race condition between three things: reloading scripts, refreshing the asset database with the reloaded scripts, and invoking the DidReloadScripts callback.

    The resource is in a file with the ".assets" extension, containing only an instance of SteamVR_Settings, which is a class defined in the same package.

    Unity 2021.3.14f1