Search Unity

Editing prefab causes references to it to become null until the object referencing it is reloaded

Discussion in 'Prefabs' started by Prodigga, Oct 12, 2018.

  1. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    719
    Hi there

    I have been unable to reproduce this issue in a clean project with the 'exact same' setup.

    In our project we have a monobehaviour, that monobehaviour references a scriptable object, and that scriptable object has a reference to a prefab.

    On our monobehaviour, we have some editor functionality to instantiate that prefab at the position of the monobehaviour. This all works fine...

    However, if I edit the prefab in any way (Turn Shadows off on the meshrenderer, turn on/off a component, anything), the reference to that prefab on the scriptable object becomes 'null' but ONLY as seen by my scripts. If I breakpoint my insantiation code, it will see that prefab reference as 'null'. But if I view the scriptableobject in the inspector, it is NOT null. It has a valid reference to the prefab. Despite 'viewing' the scriptableobject in the inspector and confirming that the reference is not null, my code still thinks it is. Any attempts made by the Monobehaviour to access myScriptableObject.PrefabReference will return null.

    I can fix this problem in one of a few ways...

    • I can clear the reference on the scriptableobject, and drag and drop the prefab back into the reference slot. This corrects the problem.
    • I can reload/recompile assemblies. This fixes the problem.
    Once I 'fix' the problem using any of the 2 options, if I edit the prefab again, it'll break again. It isn't a permanent fix.

    The follow doesn't work:
    • Right clicking and reimporting the scriptable object. This doesn't fix the problem.
    • Saving and reloading the scene. The monobehaviour in the scene still sees the prefab reference on the scriptable object as null. This doesn't fix the problem.
    • Remove the scriptable object reference on the monobehaviour and drag and dropping the scriptable object back into field on the monobehaviour. This doesn't fix the problem.
    • Our prefabs were upgraded from 2018.2.. so to ensure the prefab itself wasn't the problem I created a new prefab from scratch, and set that prefab as the referenced prefab in the scriptable asset. When I make changes to the prefab, I get the same issue. This doesn't fix the problem.
    • Our scriptable objects were upgraded from 2018.2..to to ensure the scriptable object itself wasn't the problem I created a new scriptable object from scratch and set that scriptableobject as the referenced scriptableobject on the monobehaviour. This doesn't fix the problem.
    • Using both the scriptable object created from scratch, and making it reference the prefab that was also created from scratch doesn't fix the problem either. So it doesn't seem like there is an issue with the assets themselves.
    When I breakpoint my code, and inspect the reference, it still shows me valid values. Note how the object is null in the second image, but we can still see my test variables 'ShouldBe5' and 'ShouldSayTest' reporting correct values. Also interesting to note is that the CachedPtr has become 0x0, though that doesn't mean anything to me..

    'This' is the scriptable object.
    'SpanPrefab' is the reference to the prefab.

    Before Editing 'SpanPrefab' Prefab:

    ss1.PNG
    After editing 'SpanPrefab' Prefab:

    ss2.PNG
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,410
    Thanks for bringing this to our attention. Any chance you could make the original project accessible to us and submit what you wrote here as a bug report?
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    Are your references by any chance in a List/Array/Collections?
     
  4. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    719
    No unfortunately. It's just a direct reference to script on another prefab. As you can see in the image above, there is a script called ShapeFenceGenerator which has a reference to a FencePost prefab and a FenceSpan prefab. If I edit either of these, that prefab reference suddenly becomes null.

    @LeonhardP I can give you access to our repository if you would like to pull it down and try for yourself. We do not want to publish our project via the bugtracker, since it is a bit of a black box, we don't know who is going to have access and it'll be up there in the bugtracker forever.
     
  5. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    1,410
    Could you please submit a bug report and reply with the issue ID? Then you can give the person handling your case access to your repository.
     
  6. lolun

    lolun

    Joined:
    Nov 25, 2014
    Posts:
    11
    Has this issue been submitted? It's super easy to reproduce.
     
  7. snair692

    snair692

    Joined:
    Feb 2, 2016
    Posts:
    35
    Wondering this myself I came here because I have this same thing happening.

    In my case I had a prefab on the scene that stored a list of other prefabs. When I edited the prefab from the project folder window, the prefab on the scene would get a null reference on the list and I'd have to slide the prefab back onto it.

    This kept happening, until finally I "unpacked" the prefab that was holding the list script. Now this no longer is happening. I didn't really need that to be a "prefab" anyways but it does seem like it would be easy to re-produce.

    I should say though my project migrated over from previous version of Unity. Also while not intuitive I am able to select prefabs and tell them to all update/override at once, but they don't all seem to update/override at the same time.

    I kind of like the new system, but prefab references suddenly going null is a bit... scary=p
     
  8. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    719
    Yep we still have one stuburn prefab reference on one specific script that continues to go null. We just manually fix it whenever it happens. I can't reproduce it and we do not want to upload and give an unknown Unity dev/qa person access to our entire game, so we just put up with it for now. I've tried exporting the specific scripts and the specific prefabs in question into another project to see if it happens there but no luck. Can't reproduce.
     
  9. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    We're also getting this issue. This is really very fundemental, and can be reproduced with just two scripts.

    One default Monobehaviour
    One with a serialized field and a context menu to Debug.Log the field.

    Add the 2nd script component to an object in the scene. Drag in a prefab reference to your serialized field (of type script 1). Log it - It won't be null.

    2) Edit the prefab, and save it.

    3) Log it - Now it's null.

    We've submitted a detailed bug report including a repro project. We'll need to delay releasing our next big asset until this is resolved :(
     
    jrumps and gavagai like this.
  10. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,558
    Thanks you, appreciated!

    If you post the case number here (when you have it) we can make sure to have a look soon next week.
     
    gavagai likes this.
  11. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    Sure thing :)

    (Case 1116476) Prefab References are LOST when modifying prefab

    I've also checked in the absolute latest official release and it's still occurring. Cheers!
     
  12. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @DigitalSalmon

    This is really weird. If you change the reference type in Spawner to GameObject or Transform it works fine. It is only when the type is a Monobehaviour the issue is reproducable.

    I'll have a look, but consider using GameObject as a work around for now
     
    nongbenzgames likes this.
  13. nongbenzgames

    nongbenzgames

    Joined:
    Oct 8, 2018
    Posts:
    9
    I too have references in other scripts going missing when updating prefabs. Components randomly get new FileIDs for some reason according to my Git. This has happened since the beginning of new prefab workflows to the latest 2019 alpha13. Not sure if it's due to a faulty upgrade process but this has been project breaking for months and needs attention.
     
    jrumps, trenthm and Prodigga like this.
  14. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    If it helps - AssetDatabase.ForceReserializeAssets seems to help, as does marking things as dirty then saving the asset database. Useful in small cases, but not sustainable obviously!

    Our project is a clean 2018.3 we've only been using for a few weeks so it's not the upgrade process :)
     
  15. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    Thanks, this will help in the investigation of the issue.

    Can you post your updated script here or PM to me?
     
  16. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    Ahh afraid the use of those scripts was in a live project and has since been stripped out.

    Broadly speaking, it looked like this

    Code (CSharp):
    1.  
    2. PrefabStage.prefabSaved += PrefabStage_PrefabSaved;
    3.  
    4.     public static void PrefabStage_PrefabSaved(GameObject gameObject) {
    5.             string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
    6.             IEnumerable<string> paths = allAssetPaths.Where(p => p.EndsWith(".prefab")).Where(p => AssetDatabase.LoadAssetAtPath<Module>(p) != null);
    7.             paths = paths.Concat(allAssetPaths.Where(p => p.EndsWith(".prefab")).Where(p => AssetDatabase.LoadAssetAtPath<Blueprint.Blueprint>(p) != null));
    8.             paths = paths.Concat(allAssetPaths.Where(p => p.EndsWith(".asset")).Where(p => AssetDatabase.GetMainAssetTypeAtPath(p) == typeof(Theme)));
    9.            
    10.             foreach (string path in paths) {
    11.                 Object loadedAsset = AssetDatabase.LoadMainAssetAtPath(path);
    12.                 EditorUtility.SetDirty(loadedAsset);
    13.             }
    14.            
    15.             AssetDatabase.SaveAssets();
    16.         }
    17.  
    Or instead of a dirty/save, we had previously had some success with ForceReserializeAssets, though I believe that was used in an AssetPostprocessor, or the OnWillSaveAssets in an AssetModificationProcessor.

    Sorry I can't be more specific - We tried many things, and hadn't clocked what was actually happening until we'd gone through a few 'fixes'.
     
  17. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    Ok, I get it, after saving the prefab, you mark it dirty and save it again and you reference works correctly. Meaning I missed something in the prefab system to ensure MonoBehaviour references stays valid.

    Thanks.
     
  18. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
  19. tweedie

    tweedie

    Joined:
    Apr 24, 2013
    Posts:
    261
    Any updates on this? Pretty project breaking and has prevented us moving to the new prefab workflow / 18.3
     
    Rich_A likes this.
  20. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @DigitalSalmon and others.

    It took at little while to figure out what is going but I figured it out.

    This will be a little technical.

    This is a side effect of prefabs becoming imported assets. Previously once a prefab was loaded it would never unloaded meaning references would stay just fine.

    Now when a prefab is imported the current version in memory is unloaded, the asset is processed and a new version is loaded.

    We make sure instanceid, guids and so an stay the same so any serialized references are not lost.

    BUT! due to how Unity interacts with Mono you can have situations like this one. Where one MonoBehaviour is referencing another MonoBehaviour which basically is a simple pointer in Managed memory. Each MB have a c++ backend and it is this backend that is destroy an reallocated during import, which means it will be allocated at a new memory location. But the MonoBehaviour you already have in memory will still be pointing at the old memory location because it is using raw pointers and not the instance ids.

    So in your case this now means you have reference to a managed MonoBehaviour without a native backend and thus it is null. (There was a blog post long ago about how we determine if a MonoBehaviour is null)

    I will be working with our Scripting Team to find a solution to this problem.

    For now the work around is to reference GameObjects instead, because they don't have this problem. I know it sucks but until we have a solution for the underlaying problem this is all I can offer.
     
    trenthm likes this.
  21. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    28
    @SteenLund

    1) When you say we should reference GameObject and it will work, you mean we shouldn't reference another component on the prefab, right?
    2) For the issue mentioned above, can I somehow access attached project (PrefabReferenceLostRepro.zip) and not recreate everything myself for testing, or attachment is for the stuff only?
     
    Last edited: Jan 28, 2019
    jrumps and trenthm like this.
  22. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    @SteenLund Cheers for keeping us in the loop.

    Obviously if there is anything we can do to expedite the process, please reach out - I expect this is a fairly critical bug for many, many users, so I'm sure Unity is throwing some real weight behind getting a fix out asap!
     
  23. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @DigitalSalmon

    The fix is in the top of the 18.3.6 queue so pretty much guaranteed to land there.
     
  24. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    11,786
    Oh man, thank goodness. I've just spent a good few dev hours trying to figure out what's causing a very specific bunch of references to get lost under circumstances I can't quite nail down. Fingers crossed this is the issue and 2018.3.6 clears it up!
     
  25. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    @SteenLund
    I recently updated to 2018.3.6 and this problem still seems to be present (referencing a Game Object in a Prefab field allows me to reference it... but then reverts to None/Null after closing the prefab editor).
     
  26. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    I appreciate the need for a consistent release cycle, but I think it's worth really making the point that this is a fundamental issue which I think we would all consider severe enough to merit an out-of-cycle patch fix.

    Obviously if there is still work to be done, by all means I'm sure you guys are doing a stellar job - If it's all fixed and ready to go, who do we need to lean on to get this out there so people can start using Unity again? :)
     
  27. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @Vaulcul

    Can you share a project that reproduces the issue? The original issue was fixed, I have it covered by automated tests now.
     
  28. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @DigitalSalmon

    The issue as you reported it has been fixed and I haven't seen references getting lost in any other way.
     
    trenthm likes this.
  29. DigitalSalmon

    DigitalSalmon

    Joined:
    Jul 29, 2013
    Posts:
    38
    Well, allow me to eat my words - You are correct, the issue that was submitted is fixed! Anyone experiencing a similar issue must be facing an unrelated problem, as the bug is fixed in the repro project in the bug report.

    Thank you very much for your work, great stuff :)
     
    trenthm likes this.
  30. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    @SteenLund
    My apologies, not trying to incite the wrath of others... just still having the problem that seems to be mentioned here.
    I have sent you a message with a link to my project and a description of the issue.

    Thanks for all you do for the community :)
     
  31. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    11,786
    I might have a repro case of something similar still happening. I had a bunch of references get lost last night, but am not 100% sure that they got lost after updating to 2018.3.6. I've reverted so that they're back, and will see if they disappear again.

    @SteenLund, If it does happen again then expect a multi-gigabyte repro project with unclear repro steps. With a full day of trying I couldn't lock down the circumstances under which things break.
     
  32. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    11,786
    @SteenLund, some issue very similar to this is still ocurring. I just saw a bunch of my references get dropped, and might even know steps to reproduce it.

    Bug report incoming.

    :-(
     
    Last edited: Feb 22, 2019
  33. nongbenzgames

    nongbenzgames

    Joined:
    Oct 8, 2018
    Posts:
    9
    Yes, I'm still getting this issue with lost references when applying prefab changes in the scene in 2018.3.6. Component file IDs get changed for some reason and all references to them are FUBAR. gg Untitled.png
     
  34. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @nongbenzgames

    We have seen fileids change if prefabs are saved/applied to after a player has been build. This is a brutal bug that has been fixed but it still waiting in the release queue.

    I recommend you revert your prefab to get the old fileids back and then hopefully the bug fix ships will ship next week.

    I am very sorry for this inconvenience.
     
    trenthm and nongbenzgames like this.
  35. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    719
    @SteenLund we still do not have a repo but in our project we have 4 specific prefab assets that, if referenced, the references become null/'missing' when the game is built. 2 of those prefabs are referenced by a script in our menu scene and one in our 'intro' scene (first scene in the game). There is nothing fancy or special about the reference. It is just a public GameObject field that references the asset in the project. It randomly goes null. There doesn't seem to be much of a pattern. Today, I checked the two scenes before I did my iOS build. We have our game set to run Fast with no Exceptions, so null exceptions causes crashes. Game ran fine in the editor - the prefabs were not null. Then I built the game, and the game crashed on the iOS device - null ref according to the xcode console. Checked Unity and sure enough the fields went 'missing' again. It's always the same prefabs, and we are convinced they are just forever corrupt in some way. Tomorrow I am going to try recreate the prefabs 'from scratch' to see if the error goes away, but with no reliable repro I can't even test if I fixed it properly. Just thought I'd give an update on our situation with this (?) bug.
     
  36. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    11,786
    Any ETA?
     
  37. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
  38. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    11,786
    Did this make it into 2018.3.7? I'm not seeing it in the release notes, but will check anyway when I can.
     
    trenthm and nongbenzgames like this.
  39. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    I've only done a brief test... But, it seems that this issue is fixed for me in 2018.3.7. :D Thank you for all your efforts on this.
    *****
    EDIT: I spoke too soon... While I did have one set of fields on one prefab "remembered" by Unity (Easy1), another prefab still doesn't remember (Easy2) :( Getting close it seems like... but still not fixed.
     
  40. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @Vaulcul

    Do you have a small project that reproduces the issue and can you file a bug report?
     
  41. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    ... It's the same project I had sent you earlier. I'll see if I can cut down the project to be really small and provide a new link to the smaller project. I can attempt to file a bug report, though it's really not clear to me as to why one prefab works and the other two don't... and there are no errors being thrown. so I don't know how I'd differentiate between the working and broken prefab issues to provide good information on.
     
  42. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    Oh sorry I didn’t realize it was the same project. I’ll try it out
     
  43. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    I've setup a rather basic project from scratch, not as big as the one I sent previously. I'll PM you the link if you'd like... Though if you have the original project, the same issues still happen.
     
  44. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    I would like the small one as well
    Importing the bigger one right now
     
  45. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    Okay. I just PM'd you the small file.
     
  46. Vaulcul

    Vaulcul

    Joined:
    Apr 3, 2016
    Posts:
    28
    Okay.
    So after troubleshooting my issue w/ @SteenLund, my still occurring problem was not part of this issue... I have been put on the right path and everything seems to be working well now.

    Thanks @SteenLund
     
    jrumps likes this.
  47. Jennalvw

    Jennalvw

    Joined:
    Jan 3, 2019
    Posts:
    1
    @SteenLund Hi, is this bug fixed? I am using 18.3.8, and still got this issue.
     
  48. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    436
    @Jennalvw Yes, all reference loss issues should be fixed
     
  49. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    719
    Just thought I would chime in and say that our issues are resolved, haven't seen them crop up again.
     
    angrypenguin and SteenLund like this.
  50. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    261