SerializeReference improvements in Unity 2021 LTS

SerializeReference includes support for polymorphism, which means that a field can be assigned to an instance of a class that derives from the field type. In fact, we support the field type as “System.Object” which is the root base class of every C# class. But this opens up the possibility of a successfully compiled project missing the definitions of classes that were previously available and had been saved into a scene or asset file. In some cases, classes can go missing when source files are removed, classes are renamed, or classes are moved to another assembly.

When loading a SerializedReference host object, the fully qualified type name of each managed reference object is examined and must be resolved back to a valid class type in order to instantiate it. In previous versions of Unity, missing classes could put the entire “host” object into an error state without loading any of the valid managed reference objects. So if you had a “host” with an array of 15 managed reference objects, but a single object could not be resolved, then you would not see any of them in the Inspector. There would be an error logged in the console – even though the host object was not visually marked as being in an error state when inspected – and all the edits made would be silently discarded.

In Unity 2021, we now instantiate all loadable managed reference objects and replace the missing ones by null. This gives users an opportunity to see more of the state of the host object, and to facilitate the resolution of missing types. Meanwhile, if the missing type is restored while the host object is loaded, then the triggered Domain Reload will restore the managed reference objects, and all fields referencing it will be updated properly.

This is an example of how objects with missing types appear in the Inspector:

In 2020.3, the Fruit class is missing yet the Inspector does not show any array elements, and there is no indication of an error:

In 2021.3, the Inspector warns you that the missing Fruit objects appear as null entries, whereas the Sandwich objects continue to be displayed:

The error messages in the console related to missing types have also been updated so that they’re less repetitive – they simply identify which host objects have missing types.

Here’s an error message in 2020.3:

Compare it to this warning message in 2021.3: