A scripted object has a different serialization layout when loading errors in build

When I build my Windows game I get a lot of the following errors:
The referenced script (Unknown) on this Behaviour is missing!

A scripted object (probably Animations.AnimationSequence?) has a different serialization layout when loading. (Read 44 bytes but expected 296 bytes)
Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?```

However, I have no idea where they come from, as I have no similar errors in the editor. I get the errors both when I #ifdef #endif my Editor code and when I don't, and I've run a script to check for "ghost" GameObject as described in [this article](https://gigadrillgames.com/2020/03/19/finding-missing-scripts-in-unity/) but it didn't find any. I've double checked that my Test Assemblies' Platform is set to Editor only.

As part of finding the issue, I made an empty scene and made a build with it. No errors.
I added a Mover script and ran a build. Got a bunch of errors.
Then I removed all the code from the Mover script and ran the build. No errors.
After slowly re-adding stuff to the Mover script, I found out that I only get these errors when I have *public* or *[SerializeField] private* variables in my script. *private* variables are fine.

I'm really scratching my head at this - why wouldn't it be okay to add those variables?
Here's the complete class with everything that causes an error commented out: https://hatebin.com/hliqdrrqvq 

Any help would be hugely appreciated!

None of these fields should cause that particular error.
But this script references other scripts like CharacterMover which may be where the issue is originating.

As the message says: find any instances of scripts where you #if away any serialized fields (public or SerializeField). And particularly look at those used by Animations.AnimationSequence.

Also try making a clean build.

hi BluezamX,

did you find a solution to your problem ?

Hi all - so having just spent a whole day working through the same error, it seems there are various causes. For me, the fix was not related to #if UNITY_EDITOR nor did it turn out to be related to the specific files mentioned in the error msg we are all seeing.

I am using .asmdef assembly references. It turned out a few of them had reference to Unity’s “testrunner” files (which I don’t remember even adding!) What worked for me is:

  1. Removing the references to anything to do with unity Testrunner (there were 2 such ref’s in my asmdefs)
  2. Scroll down the .asmdef window, and near bottom there is a box for “Define Constraints” and even after I completed #1, the box still had UNITY_INCLUDE_TESTS. Delete that.

After doing this to every one of my .asmdefs, it finally built. What a nightmare - I hope this helps someone someday!