Objects created in editor tool, fields reset on play and unable to serialize.

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. :slight_smile:

EDIT: On second thought, LinkedList< T > does have the Serializable attribute… And still it is being reset! Why is this? :open_mouth:

As you have guessed, Unity cannot serialize everything, it can serialize classes with the Serializable attribute and (according to the docs, but it seems to work fine without) inheriting from UnityEngine.Object. Unity must however be able to serialize the members of the class, which could be ints,floats,strings,booleans, other similar simple types or built-in arrays of those (e.g float).

If Unity should be able to serialize your data, you must expose them to a public variable in a ScriptableObject or to a MonoBehaviour.
Unity should be able to serialize a linked list of simple classes like this:

public TestClass linkedListStart = new TestClass ();

[System.SerializableAttribute]
public class TestClass {
	public float someData = 42;
	//Note, if this was set to = new TestClass (), it would get stuck in an infinite loop and crash Unity
	public TestClass next = null;
}

Try to organize your data to something Unity can serialize, that is the easiest approach.
And don’t forget to mark your object with EditorUtility.SetDirty (…);

Otherwise you can use a custom serializer to serialize your data.
Take a look here: Object Serialization in .NET | Microsoft Learn

LinkedList should be serializable, as long as it is storing a serializable type.
Are there any other types you are depending on that are truly not serializable?
Check the documentation page for [SerializeField].

Try setting [System.Serializable] on the objects you are creating from the editor.

[System.Serializable]
public class MyClass
{
    [SerializeField]
    private LinkedList<int> linkedList;
}