Hello peopleses!
Sorry for the long read. I do have a question, but if this is better suited for a Forum discussion, I apologize.
I’m using an editor tool of mine to create objects (through code) in the editor before the game start, and it seems to work just fine at first. But as soon as I press play I get all these NullReferenceExceptions. It seems that some fields, and all my non-public fields, are being reset upon pressing play.
After some confusion and searching for this problem this my understanding of the case: objects are serialized and recreated on start. As a result, if something is not serializable it is given a default value (null or zero, etc). So it seems what I need to do is serialize all (non-public) fields so that Unity can create them again on play.
But… You don’t seem to be able to serialize everything. For example, I’m using LinkedList, and while I can serialize the field holding an object, that object’s LinkedList fields are set to null. This is for a big project, big classes, lots of code - changing the classes being used is not an option.
So my question: Is there an easy workaround to this problem? Is there another way to get your objects from the editor to the game without having all fields, fields of the fields’ objects (and so on) serializable? Since some of my objects unfortunately are not fully serializable it seems.
I can think of one workaround: I could make new representations of all the data currently used to create the objects in the editor, and have these representations be serializable. And instead of creating the objects directly in the editor I create objects for holding the serializable versions of the data. Then on Start() I create the actual objects using this data. (A matter of convering the data from one format to the other and then back.)
But maybe I’m missing something. Any suggestions, guidance or corrections to this problem is greatly appreciated!
Cheers. ![]()
EDIT: On second thought, LinkedList< T > does have the Serializable attribute… And still it is being reset! Why is this? ![]()