Unity-Technologies/UnityCsReference · error · ArgumentNullException

Value cannot be null. (Parameter 'additionalCompilerArgument

Error message

Value cannot be null. (Parameter 'additionalCompilerArguments')

What it means

Thrown by PlayerSettings.SetAdditionalCompilerArguments(NamedBuildTarget buildTarget, string[] additionalCompilerArguments) when the array is null. The method forwards compiler arguments to the native SetAdditionalCompilerArgumentsInternal, which requires a valid (possibly empty) array. An empty array clears existing arguments; null is not a valid sentinel.

Source

Thrown at Editor/Mono/PlayerSettings.bindings.cs:1112

                throw new ArgumentNullException(nameof(defines));

            SetScriptingDefineSymbols(buildTarget, ScriptingDefinesHelper.ConvertScriptingDefineArrayToString(defines));
        }

        [StaticAccessor("GetPlayerSettings().GetEditorOnlyForUpdate()")]
        [NativeMethod("SetUserScriptingDefineSymbols", ThrowsException = true)]
        private static extern void SetScriptingDefineSymbolsInternal(string buildTargetName, string defines);

        [StaticAccessor("GetPlayerSettings().GetEditorOnly()")]
        [NativeMethod("GetAdditionalCompilerArguments", ThrowsException = true)]
        private static extern string[] GetAdditionalCompilerArgumentsInternal(string buildTargetName);
        public static string[] GetAdditionalCompilerArguments(NamedBuildTarget buildTarget) =>
            GetAdditionalCompilerArgumentsInternal(buildTarget.TargetName);

        public static void SetAdditionalCompilerArguments(NamedBuildTarget buildTarget, string[] additionalCompilerArguments)
        {
            if (additionalCompilerArguments == null)
                throw new ArgumentNullException(nameof(additionalCompilerArguments));

            SetAdditionalCompilerArgumentsInternal(buildTarget.TargetName, additionalCompilerArguments);
        }

        [StaticAccessor("GetPlayerSettings().GetEditorOnlyForUpdate()")]
        [NativeMethod("SetAdditionalCompilerArguments", ThrowsException = true)]
        private static extern void SetAdditionalCompilerArgumentsInternal(string buildTargetName, string[] additionalCompilerArguments);

        [StaticAccessor("GetPlayerSettings().GetEditorOnly()")]
        [NativeMethod("GetPlatformArchitecture", ThrowsException = true)]
        private static extern int GetArchitectureInternal(string buildTargetName);
        public static int GetArchitecture(NamedBuildTarget buildTarget) => GetArchitectureInternal(buildTarget.TargetName);

        [StaticAccessor("GetPlayerSettings().GetEditorOnlyForUpdate()")]
        [NativeMethod("SetPlatformArchitecture", ThrowsException = true)]
        private static extern void SetArchitectureInternal(string buildTargetName, int architecture);
        public static void SetArchitecture(NamedBuildTarget buildTarget, int architecture) =>
            SetArchitectureInternal(buildTarget.TargetName, architecture);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass Array.Empty<string>() instead of null when you want no additional compiler arguments.
  2. Null-coalesce at the call site: SetAdditionalCompilerArguments(target, args ?? Array.Empty<string>()).
  3. Initialize the collection eagerly: var args = new List<string>(); then convert with .ToArray() which is never null.

Example fix

// before
PlayerSettings.SetAdditionalCompilerArguments(target, null);

// after
PlayerSettings.SetAdditionalCompilerArguments(target, args ?? Array.Empty<string>());
Defensive patterns

Strategy: validation

Validate before calling

PlayerSettings.SetAdditionalCompilerArguments(target, args ?? Array.Empty<string>());

Prevention

When it happens

Trigger: Calling SetAdditionalCompilerArguments with a null string[] — for instance when a configuration source returns null for 'no additional arguments' instead of an empty array.

Common situations: Build pipeline scripts reading compiler arguments from config files that may be absent; CI/CD scripts that pass null when a feature flag is off; deserialization of compiler argument lists that are null by default.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/ee9d6f8bca110d95. Report an issue: GitHub.