We’re having an issue where a build step in our custom build system fails to execute when running the build in batch mode on our build server. I’ve traced the issue down and determined that it’s caused by a serialized reference that is null only when running in batch mode. Verified this by logging the value of the reference during the build and comparing the results when running in batch mode and in editor.
The reference in question is located in our BuildConfiguration asset, which contains all the data that tells our build pipeline how to build the game. The only difference between when we run the build in batch mode vs in the editor is that in batch mode the BuildConfiguration asset is loaded using AssetDatabase.LoadAssetAtPath (from a static method that is invoked from the command line), while in the editor we just hit a button in the inspector of the asset to start the build. To be clear: the BuildConfiguration asset loads fine, but another asset referenced from it does not.
I tried disabling Unity Accelerator to make sure it wasn’t an issue with the asset cache, but that did no difference.
My best guess is that the asset database is not ready for some reason and fails to load that specific reference.
The problem may be related to this… in the “hit a button” case, you clearly have the object already stood up in memory because you’re looking at it in the inspector. Does the button code access or set anything up or does it just call the next step of the build?
Also, what happens if you replace the AssetDatabase load call you are using with Resources.Load() and then move the asset over to a Resources/ folder?
We loaded the BuildConfiguration asset in a static method and passed it on from method to method as a parameter without ever storing a reference to it in a persistent location, such as a static field. Since the build process takes a long time to finish, Unity presumably has time to cleanup unused assets before the whole thing is finished (likely even does so as part of the build to conserve memory). When building manually from the editor, however, the asset wouldn’t unload since it was open in the inspector.
Solved the issue by storing a reference to the asset in a static field so that Unity can see that it is still in use. I guess the lesson learned is that Unity’s asset cleanup doesn’t care about references on the stack.