Live recompiling causing null property to be initialized... Without using its constructor
I have a class that contains a property that is normally null. When I recompile my project live (as in, change a script while in play mode), it suddenly is no longer null and I determined that the constructor for that type is not being called when this happens.
I did a little research and it seems the only way to do this is with FormatterServices.GetUninitializedObject(). My question is 1) Why is unity bothering to set a non-serializable field, and 2) apart from giving every property a backer field, what are my options?
Answer by RebelBinary · Dec 02, 2020 at 04:11 AM
Globals get set to null, as well as not serialized data types like dictionaries
To fix this, catch the live recompile on a OnDisable (to serialize anything that won't survive) and on OnEnable, reset your globals by setting the instances to this and re-create complex data objects
For example I had a dictionary that I would turn into a list on disable, and then re-establish the dictionary on OnEnable