I’m trying SO’s for the first time in a new side-project. At first I felt them a very capable and exciting feature, but now it seems they have some notable limits and break in actual builds and must be used cautiously.
The attached is a debug project. I use an SO motion script as a way to assign a motion type to an agent, such as follow player or fly straight or follow a path. Running “test.scene”, the bug follows a path (created with the asset BGCurve) assigned at runtime. But build for Windows or Android, the bug does nothing, and the debug log records:
The referenced script on this Behaviour (Game Object ‘’) is missing!
Furthermore, I can get Unity to lose reference to the SO script so the project fails in the Editor. After building an executable, make a change to the EntityMotion_Path script such as adding a log. It runs fine in the Editor with the change. Exit the editor, reload the project, and the SO script reference is missing. This break only happens after a build; you can merrily make changes to the EntityMotion_Path code without the script reference being lost so long as you haven’t build the project. Thus the user experience is:
- Get game running great in Editor.
- Build and it does nothing.
- Try some debugging and get nowhere.
- Find the game doesn’t even run in the editor any more either!
- optional - rant a bit about wasted effort and WTF is going on and how can the game suddenly stop working?!?!
Within my game proper that I’m trying to debug, I get the error:
A scripted object (script unknown or not yet loaded) has a different serialization layout when loading. (Read 44 bytes but expected 72 bytes)
Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?
There’s a known issue with #if UNITY_EDITOR directives creating conditional [SerializeField] Inspector fields. The only code calling this in my project is BGCurve, which uses it to create the interface.
Best I can gather, using conditional serialised fields in Scriptable Objects causes them to break. I shall proceed to replace my SO motions with an enumerated type and use a conditional code selector and see if that works.
I don’t know that this warrants an official bug report. All mention of the Serialization issue says it’s by design and it’s been present for years. It just seems that the way Unity works and builds projects means it cannot use SO with UNITY_EDITOR [SerializeFields] directives.
7571374–937615–DebugTest.7z (254 KB)