- Home /
How to change script load order? (Serialization problem)
I have a series of classes that use public static instances of themselves to mimic the enum functionality offered by Java. As you might imagine, there were some tricks to serializing and deserializing those static instances, but I have that sorted out now. My test objects save and load from disk correctly.
Unfortunately, when I apply the [Serializable]
attribute to my character class, I run into several variations of the following error on startup:
NullReferenceException: Object reference not set to an instance of an object
Namespace.DurationType..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Namespace.DurationType
It appears that putting the [Serializable]
attribute on this particular class causes something on it to be executed before anything else in my codebase. This is very confusing to me, since the null error that is logged suggests that the static constructor on my enum classes has not been called properly.
Is there a way to force a certain script to be handled first by Unity? If so, I would expect I could force my static constructor to run before this weirdness occurs with the character class.
Note: none of the classes I'm serializing are derived from MonoBehaviour.
Update: The problem appears to be caused by Unity creating an instance of one of my classes before allowing its type initializer to be run. This is caused by having a script in the editor reference a class that uses my character object.
If I use a startup script to attach the character object dynamically, the problem goes away.
This is pretty clearly a Unity bug. Still looking for a better workaround than "script everything instead of using the editor".
Answer by RedDevil · Aug 13, 2014 at 05:57 AM
Yes there is...in fact you can make all scripts go in what order you want.I was experiencing a similar issue. GO to EDIT->Project Settings->Script Execution Order and there you can add or remove what script goes first from top to bottom. :)
I put my initialization script at -10000 in the script execution order settings. I also adding a logging message at the top of its Awake() and OnEnable() methods.
Whatever execution is occurring on my Character class is happening before the script marked to execute first. If I remove the [Serializable] attribute from Character the problem goes away and the game loads properly. (But I can't save it...)
Answer by CypherJH · Apr 01, 2017 at 06:59 AM
Little late, but... Another solution would be to use Awake, instead of Start, since this is handle before any Start function, according to the monobehaviour lifecycle diagram.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to assign a class ID at runtime? 0 Answers
How to control serialized variables over the inspector pane? 1 Answer
Script not executed when upgrading to Unity 4.3 0 Answers
Serialized class to google games services cloud save 0 Answers