Search Unity

Feedback Resources.Load fail on 2020.3.46f1 sometimes fail due to being destroyed, no documentation on it

Discussion in 'Editor & General Support' started by Guedez, Jun 11, 2023.

  1. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    One of my assets decided it will not work anymore, it has been working just fine for months, and one day it started not loading through Resources.Load anymore.

    It does get added to the build

     9.0 kb    0.0% Assets/Everyday Engine/src/SceneMods/EndDayScene/Resources/MoneyReport/MoneyReport UXML.uxml
    9.0 kb 0.0% Assets/Everyday Engine/Resources/Plants/Strawberry Plant/Prefabs/Strawberry_Plant M4 (Group).prefab < this one
    8.9 kb 0.0% Assets/Everyday Engine/Resources/Models/Buildings/Stone Large Tower Door Lower.blend


    It works just fine in the editor, but it refuses to work in the built player.


    Entity prefabEntity = default;
    int count = 1;
    GameObject original = Resources.Load<GameObject>(variation.Prefab);
    if (original == null) {
    Debug.LogException(new Exception("Plant data not found: " + variation.Prefab));
    counts[(CROP, variation.Size)] = 1;
    return default;
    }


    As you can see, the path I am calling is perfectly correct, and it has been working for months, but since 3 days ago, it simply stopped working out of nowhere. I've tried deleting the library folder, reimporting all assets, it simply does not work anymore. And worse of all, there could be more of these I don't know about yet.

    upload_2023-6-11_12-56-33.png

    upload_2023-6-11_12-56-49.png

    upload_2023-6-11_12-57-20.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Are you using source control? If so, see what changed!

    Otherwise, copy the prefab, call it
    foo.prefab
    , put it directly in a Resources/ folder and change its name to
    "foo"


    That will isolate naming issues from corruption issues in the prefab itself. I assume you can open and edit it still?

    Here is the best way to immunize your project to this stuff:

    PROPERLY CONFIGURING AND USING ENTERPRISE SOURCE CONTROL

    I'm sorry you've had this issue. Please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    I usually make a separate repository for each game, but I have some repositories with a bunch of smaller test games.

    Here is how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally the ONLY folders you should ever source control are:

    Assets/
    ProjectSettings/
    Packages/

    NEVER source control Library/ or Temp/ or Logs/
    NEVER source control anything from Visual Studio (.vs, .csproj, none of that noise)

    Setting git up with Unity (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place. Digital storage is so unbelievably cheap today that you can buy gigabytes of flash drive storage for about the price of a cup of coffee. It's simply ridiculous not to back up.

    If you plan on joining the software industry, you will be required and expected to know how to use source control.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards
     
  3. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Yeah, I use git and that's pretty much my configuration.
    I can still open and edit.
    The copy on the Resources root works, the original does not.
     
  4. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Code (CSharp):
    1.  
    2. public static AnimationManager.InstanceAnimationInfo GetAnimData(PlantVariation p) {
    3. AnimationManager.InstanceAnimationInfo anim;
    4. if (p.Prefab == null) {
    5. return null;
    6. }
    7. if (!Cache.TryGetValue(p.Prefab, out anim)) {
    8. GameObject prefab = Resources.Load<GameObject>(p.Prefab);
    9. if (prefab != null) {
    10. AnimationInstancing.AnimationInstancing[] a = prefab.GetComponentsInChildren<AnimationInstancing.AnimationInstancing>(true);
    11. for (int i = 0; i < a.Length; i++) {
    12. try {
    13. anim = AnimationManager.Instance.GetOrCreateAnimationInfoFromFile(a[i].GetAnimationName);
    14. break;
    15. } catch { }
    16. }
    17. }
    18. Cache[p.Prefab] = anim;
    19. //Utils.ContextDestroy(prefab);
    20. }
    21. return anim;
    22. }
    23.  
    This was the offending code... which has not changed for months, neither has anything that calls it.
    I guess I will rename this thread to "Add a error if you try to load a destroyed resource" or at least any indication that this could be the issue in the documentation. As for why this has never been an issue until some days ago.. it's a mystery
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    It already complains if you Destroy() assets.

    Try Destroy()-ing a prefab.

    Screen Shot 2023-06-11 at 1.04.39 PM.png

    Try it yourself, even Steve Martin agrees:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker
    4.  
    5. public class HeHatesThesePrefabs_StayAwayFromThePrefabs : MonoBehaviour
    6. {
    7.     // https://youtu.be/Tcwz8-EfFYE
    8.  
    9.     public GameObject Prefab;
    10.  
    11.     void Start ()
    12.     {
    13.         Destroy(Prefab);    
    14.     }
    15. }
     
  6. Guedez

    Guedez

    Joined:
    Jun 1, 2012
    Posts:
    827
    Not really. Not a single error in editor. Same code. But breaks on build.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    I feel your pain... you're basically telling Unity to Destroy() something that is a read-only part of your project, so it kinda sorta did its best and returns you a null...

    I know that's weird, but in a "whacky edge case of software" context, it is correct.

    I mean you are technically far beyond the sticky edge of the envelope there attempting to blow up a read-only thing. :)

    But yeah, it would be nice if it complained in build.