We finally have a fix to an elusive issue that we know has caused frustration for many of you for a while. MonoBehaviour-based components as well as ScriptableObjects would lose references to other objects under certain conditions when the scripts they were based on temporarily didn’t compile.
This could for example happen under the following conditions:
-
A team member fetches updates from version control where a script component has a new field and a Prefab that uses the script has a reference to another object assigned in that field.
-
However, due to local changes, the updated script doesn’t compile. This means the Prefab cannot import and so the added field is not present on the Prefab.
-
Once the compile error is fixed, a reimport of the Prefab can make the reference appear again. Unity keeps track of missing imports of Prefabs so it can automatically reimport them once the compile errors have been fixed. However, this information about missing imports is lost if the editor is restarted. In this case, the reimport does not happen automatically after all, and the reference remains missing even though there are no longer any compile errors.
Usually, the issue could be worked around by reimporting the affected Prefabs or other objects manually. However, tracking down the issue is not always trivial, and is made worse by the fact that it can manifest differently for different people working on the same project. Some of you have resorted to simply reimporting all Prefabs to be safe, which can take a while, especially for large projects.
A robust Asset dependency-based fix
The fix required introducing a new form of dependency in the Asset Database. Prefabs and other Assets now have a script type dependency on the MonoBehaviors and ScriptableObjects they contain instances of. Like with other Asset dependencies, this means that if a script type is changed, the Assets that depend on that script are reimported.
Luckily, it’s not simply any edit of a script that causes dependent Assets to be reimported. It only happens if the fields of the script change, meaning fields are added or removed, or existing fields change name, type, or order. Changes to methods or properties won’t cause any imports of dependent Assets to happen.
This change is needed because the imported result of Prefabs and other types that contain MonoBehaviors or ScriptableObjects depend on the fields of those scripts. So when the fields on those scripts change, a reimport must happen for the imported result to reflect those changes to the scripts.
It’s worth being aware that the new dependency will cause Prefabs to get re-imported upon script changes in cases where it didn’t happen before. This is a necessity of the more robust way of ensuring Prefabs get imported when they need to, in order to avoid missing references.
Versions of Unity with the fix
The fix will be coming to Unity 2020.1.0b10. Additionally, we are backporting it to Unity 2019.3, where it should become available in a couple of weeks. Since the fix depends on infrastructure that’s part of AssetDatabase v2 which was introduced in 2019.3, it’s not possible to backport the fix further back than 2019.3.