Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Resources.Load not working unless any asset is reimported

Discussion in 'Scripting' started by dani-unity-dev, Dec 30, 2020.

  1. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    After upgrading a project from 2019.4 to 2020.2 I am experiencing an issue with the Resources class.

    When the editor is launched a call to Resources.Load that happens in Awake fails, unless I reimport any asset in the project. I don't really need to reimport the asset I am trying to load (a prefab) but any asset in the project, even a texture. To be even more clear: the path of the prefab is correct and the code did not change at all, only the editor version changed. Builds are not affected by this.

    Thanks for any help.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Any time you upgrade you should make a full back up of your project and then delete the Library folder in your project (with Unity closed), and then reopen Unity to let it reimport all.

    The Library folder is where Unity builds the "partially built stuff" that ultimately goes into your game.

    When you upgrade versions, you generally want Unity to recreate all this stuff again. This is the same as doing a Reimport-All from your project, but it forces Unity to be honest and not have bugs.
     
    masterton likes this.
  4. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Hi, I do have a backup (git) and I have also tried deleting the lib folder already, I should have pointed that out. Actually I have already reverted to 2019 but I was hoping somebody else experienced something like this and could help. Thanks for your time anyway.
     
  5. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    It happens when I re-open the project and every time I have to do the re-import thingy. And I am not using addressables so far, although it's a dependency for another package I am using.
     
  6. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
  7. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Also, a workaround is changing Zenject so it loads that prefab using the AssetDatabase #if UNITY_EDITOR. That works and in builds the issue doesn't seem to repro, so it should be fine. But it's very hacky.
     
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    I'm not using Zenject, so that's probably not directly related. It seems like more of an underlying issue.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Well, not to start a religious war or anything, but I found everything about Zenject to be hacky.

    It seemed like a computer science construct that sounded really good on paper, but turned your project into a mess.

    We used ZJ for about two years on a big project and it made tracking down initialization errors 10x times harder than without it.

    Ultimately we abandoned it in favor of a traditional explicit order-of-initialization mechanism, and everything is so much better now.

    And when ZJ was present, it caused all manner of weird lifecycle edge case things, which honestly sounds exactly like what you're getting above.
     
    LorenzoValente and masterton like this.
  10. jring

    jring

    Joined:
    Dec 2, 2015
    Posts:
    7
    I just upgraded from the latest 2018 LTS to the latest 2019 LTS and ran into this issue straight away. It looks like it might be a bug with Asset Database V2, because I can reliably make the issue go away by rolling back to V1. Might be worth a try on your project just to confirm that's where the problem lies.
     
  11. Jamez0r

    Jamez0r

    Joined:
    Jul 29, 2019
    Posts:
    205
    Also have this issue. Would be great to have a permanent fix.
     
  12. RisingSunStudios

    RisingSunStudios

    Joined:
    May 23, 2018
    Posts:
    34
    Same here. Not using Zenject, but StrangeIOC. This is with Unity 2020. It fails to find a simple text asset file.
     
  13. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    A lot of constructors in Unity are NOT called by the main thread.

    Most of the Unity API can ONLY be used from the main thread.

    Because of how these IOC are designed to emergently work based on dependency rules you set up, it can be very easy to call a bunch of stuff NOT on the main thread in Unity, and then that stuff... doesn't work, because it isn't supposed to work off the main thread.