Overriden SerializeReference Fields don't recover missing references

Hi. So I found a bug that can cause a many hours work to be lost when using SerializeReference. Normally, when a class assigned to a SerializeReference gets renamed (or it’s namespace, or it’s assembly), Unity will warn you to fix the problem. Usually, one can fix it by renaming the class back, or by using the MovedFromAttribute. But, in some cases, when the class is used in a prefab override, the reference is set to null, and it’s data can be lost forever. The worst part is that this bug is silent, so one may not notice its effects until it’s too late to do something about it.

I’ve already reported this bug to Unity. Here’s a link to the Issue Tracker. Please vote to fix it if you are using SerializeReference. Even if you aren’t using it directly, it’s used by multiple plugins in the Asset Store, and even some Unity ones, like the Localization package, and it could cause you to lose a lot of work too.

I’ve attached a zip with everything necessary to reproduce this bug. Just add the files to a Unity Project and follow these steps:

  • Open SceneA. Select the SRGameObject and notice it has a couple of Managed Reference fields with data inside them. There’s also a SRPrefabInstance with Managed References with data too.

  • Open SceneB. This is to ensure SceneA is closed.

  • Open the SRBugDemonstrator script and rename the StringContainer class to something else. You’ll see a bunch of notifications in the console about not finding a class.

  • Open SceneA again. Notice that some of the data in the Managed References is missing; this is normal.

  • Rename the StringContainer class back to it’s original name.

  • Notice that SRPrefabInstance is still missing some data, but SRGameObject is not.

To repeat the experiment, you can make things go back by right clicking SceneA and reimporting it. But if you make any changes in the affected GameObjects, and you save those changes, reimporting won’t work anymore. It’s easy for this to happen in real life. A simple mistake could introduce a lot of data loss that isn’t noticed until much later.

This also happens with Prefab Variants, and I’ve included one to reproduce the error. But you’d need to close Unity and open it again instead of just closing SceneA, because that’s the only way to unload the prefab. That said, opening a project with missing classes because a package you are using renamed something is a common way in which this bug may be triggered in real life.

EDIT
If someone from Unity is reading this, I offer my time and I’d happily help with any information and answering questions if you need to clarify something about this bug. :slight_smile: Thank you.

EDIT 2
For some reason, the bug won’t happen if the original prefab asset is also missing a reference.

9898842–1429578–SR_Bug_In_Prefabs.zip (9.68 KB)

1 Like

UPDATE
Unity closed the issue. It’s marked as “Won’t fix”. I think it’s sad :disappointed_face:. It took me a while to find the exact bug and to make a clear reproduction example.

SerializeReference really needs better, official support for renaming types. It’s frequently recommended to use the MovedFromAttribute for that, but it’s impractical to use when renaming namespaces or assemblies.

To make things worse, now it’s been decided that the MovedFromAttribute won’t fix prefab instances sometimes. The worst part is that most users won’t even notice this bug when it happens; they’ll just notice they’ve lost a bunch of work some time later. Those kinds of bugs are awful. There is a workaround (reimport scenes and prefab variants before editing anything when this bug happens), but if it’s hard for users to understand this bug, it’ll be even harder for them to know how to handle it.

More in general, SerializeReference really needs some love. In addition to the problems when renaming types, it has multiple other bugs. There are several other problems with prefab overrides that can cause lost work too. Also, its support for multi-object editing is so buggy; it’s easy for it to print errors in the console, and it’s made the Editor crash. I’m too discouraged now to report more SerializeRefence bugs, but I hope this awesome feature receives the attention it needs one day.

2 Likes

SerializeReference really is a bit of a second or third class citizen right now. Been around since Unity 2019 and there’s still no built-in inspector support for it. That alone is almost criminal.

Its a big shame considering how much of a major impact it can have with improving your code architecture.

3 Likes