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.
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)