Unity FMOD plugin shows error when building the game

Hi,

When I’m trying to build our game I’m getting this errror:

NullReferenceException: Object reference not set to an instance of an object
FMODUnity.EditorSettings.PreprocessStaticPlugins (FMODUnity.Platform platform, UnityEditor.BuildTarget target) (at Assets/Plugins/FMOD/src/Editor/EditorSettings.cs:513)

and

NullReferenceException: Object reference not set to an instance of an object
FMODUnity.EditorSettings.PostprocessBuild (UnityEditor.BuildTarget target) (at Assets/Plugins/FMOD/src/Editor/EditorSettings.cs:494)

From what I’ve found the “Platform platform” variable is null. When I do any change in any class of the project and the project will recompile, the errors are gone and the project is building correctly.
However we use Unity Cloud Build where the same error occurs and a workaround with recompiling is not possible.

Are there any solutions to the problem?
If that is any helpful we don’t build the project with “Build” button in “BuildSettings” window. We do have our custom build methods and we call: BuildPipeline.BuildPlayer() method by ourselves.

I am using Unity 2021.2.17f1 and FMOD 2.02.04

Are you switching build target in your build script, i.e calling EditorUserBuildSettings.SwitchActiveBuildTarget?
If you are then you can instead use EditorUserBuildSettings.SwitchActiveBuildTargetAsync which will recompile scripts and fire a callback when script compilation is finished, and in that callback you can call BuildPipeline.BuildPlayer.
Otherwise, just to help me debug this issue, can you please tell me whether there are any platforms listed in the “Platform Specific” section of your Unity FMOD Settings?

I 'm not using EditorUserBuildSettings.SwitchActiveBuildTarget or EditorUserBuildSettings.SwitchActiveBuildTargetAsync right know, because I’m building for Windows, and the editor as well as Unity Cloud build is setup for Windows builds.

I tried to add EditorUserBuildSettings.SwitchActiveBuildTargetAsync call before the build, but with no success. In Unity Cloud Build I’m still getting same error:

My current FMOD Settings are listed below


When I’m selecting “Editor” right after I open the Unity Editor I’m getting such errors as well (I don’t know if it is somehow connected to the problem):

I think that would be connected to the problem. Would it be possible for you to upload a stripped down reproduction to your FMOD Profile? Just a project with all the assets removed except the Plugins/FMOD directory and some built banks should be sufficient to reproduce this.

Hey @jeff_fmod

Sorry for long reply, but I had a lot going on around the project.

I managed to strip out the whole project and leave only the necessary elements to test it out. After removing lots of stuff the problem with error in the FmodStudioSettings is gone, but the original errors with PreprocessStaticPlugins and PostprocessBuild still occur.

I cannot share the repro project publicly, but I will contact you by PM with all information. Is that okay?

That sounds like a good reproduction, thank you for taking the time to get that setup. Uploading it to your FMOD Profiler is private and secure, but feel free to pm me a link if you prefer.

This issue appears to be due to the Library folder being refreshed during a Unity Cloud build. I can reproduce this error by deleting the Library directory and immediately attempting to build. Recompiling gets it back into a good state, but this is not possible during a Unity Cloud build, so I have passed this onto the Dev team to investigate further.
EDIT: As a possible workaround, I have found that forcing a recompile using EditorUserBuildSettings.SwitchActiveBuildTargetAsync allows it to build succesfully after the Library directory is deleted. Here is an example script:

#if UNITY_EDITOR

using UnityEditor.Build;
using UnityEditor;

public class Build : IActiveBuildTargetChanged
{
    private static string destPath = "";
    public int callbackOrder { get { return 0; } }

    [MenuItem("Tools/Switch and Build")]
    public static void SwitchAndBuild()
    {
        destPath = EditorUtility.OpenFolderPanel("Choose Build Destination", "", "");

        if (string.IsNullOrWhiteSpace(destPath))
        {
            return;
        }

        /* Switch to a different platfrom to start with, just so we can switch back to Windows
         * and leverage the recompile in SwitchActiveBuildTargetAsync
         */
        BuildTarget target = BuildTarget.WebGL;
        BuildTargetGroup targetGroup = BuildTargetGroup.WebGL;
        EditorUserBuildSettings.SwitchActiveBuildTarget(targetGroup, target);

        target = BuildTarget.StandaloneWindows64;
        targetGroup = BuildTargetGroup.Standalone;

        EditorUserBuildSettings.SwitchActiveBuildTargetAsync(targetGroup, target);
    }

    public void OnActiveBuildTargetChanged(BuildTarget previousTarget, BuildTarget newTarget)
    {
        BuildPlayerOptions options = new BuildPlayerOptions();

        options.scenes = new[] { "Assets/Scenes/SampleScene.unity" };
        options.locationPathName = destPath + "\\Build.exe";

        BuildTarget target = BuildTarget.StandaloneWindows64;
        BuildTargetGroup targetGroup = BuildTargetGroup.Standalone;

        options.target = target;
        options.targetGroup = targetGroup;

        BuildPipeline.BuildPlayer(options);

        UnityEngine.Debug.Log($"Finished building!");
    }
}
#endif