Search Issue Tracker
By Design
Votes
1
Found in
2019.3.0a1
2019.3.15f1
2020.1
2020.2
Issue ID
1252121
Regression
No
OnAfterDeserialize is not called when disabling domain reload and scene reload
How to reproduce:
1. Open attached project "case_1252121-bugreport.zip"
2. Open "SampleScene" Scene
3. Enter Play Mode
4. Observe the error message thrown in the Console window
Expected result: reference to a Prefab remains unchanged
Actual result: reference to a Prefab becomes null
Reproducible with: 2019.3.0a1, 2019.4.0f1, 2020.1.0b13, 2020.2.0a15
Not reproducible with: 2018.4.23f1, 2019.2.21f1
Error message:
MissingReferenceException: The object of type 'Character' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Notes:
Assets > Data > CharacterSet has two references attached as seen in the Inspector window under the "Data" dropdown. "CharacterSet.cs" script contains the OnValidate method, in which a Debug.Log prints if the object exists or not. After entering Play Mode, the elements of "Data" become null and a script recompile is needed to fix the issue.
Any domain reload will fix this issue. Closing the project and nuking the Library folder will make it re-appear.
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- [MacOS] Crash on [NSApplication endModalSession:] when saving while Play Mode is loading
- Importing a theme through the Inspector allows importing the same theme multiple times
- Editor window disappears when repositioned in a specific way in Editor
- Importing a theme through the Inspector allows circular import
- FocusNextInDirection not focusing when used on multiline TextFields
Resolution Note:
By design, OnAfterDeserialize is not called when disabling domain reload and scene reload. As this would make the feature slower due to having to recursively visit all serialized objects and invoke the callback. It also not required if the serialization callbacks are only used for serialization, as the values are kept/not being overwritten when domain reload and scene reload are disabled.
See docs for details: https://docs.unity3d.com/Manual/ConfigurableEnterPlayModeDetails.html
OnBeforeSerialize and OnAfterDeserialize should only be used for serialization purposes and should always be symmetric, e.g. save values in serialized fields in OnBeforeSerialize and load/restore them in OnAfterDeserialize. As in the sample code: https://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html
For initialize and shutdown of non-serialization code, the symmetric MonoBehaviour/ScriptableObject methods Awake/OnDestroy and OnEnable/OnDisable should be used.