Unity-Technologies/UnityCsReference · error · ArgumentNullException

settings

Error message

settings

What it means

Thrown by ReplacePrefabAssetOfPrefabInstances (the overload taking PrefabReplacingSettings) when the settings parameter is null. PrefabReplacingSettings controls how overrides and object references are handled during the batch replace; a null value is not permitted — the simpler overload constructs a default PrefabReplacingSettings internally.

Source

Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:2289

                    throw new InvalidOperationException(string.Format($"Cannot replace the Prefab instance when it has a missing script. GameObject '{gameObjectsWithInvalidScript[0].name}' has a missing script. Use InteractionMode.AutomatedAction to force the replace."));
            }
        }

        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, InteractionMode mode)
        {
            ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, new PrefabReplacingSettings(), mode);
        }

        public static void ReplacePrefabAssetOfPrefabInstances(GameObject[] prefabInstanceRoots, GameObject prefabAssetRoot, PrefabReplacingSettings settings, InteractionMode mode)
        {
            if (prefabInstanceRoots == null)
                throw new ArgumentNullException(nameof(prefabInstanceRoots));

            if (prefabInstanceRoots.Length == 0)
                throw new ArgumentException(nameof(prefabInstanceRoots) + " has no objects");

            if (settings == null)
                throw new ArgumentNullException(nameof(settings));

            ThrowIfInvalidAssetForReplacePrefabInstance(prefabAssetRoot, mode);
            foreach (var go in prefabInstanceRoots)
                ThrowIfInvalidArgumentsForReplacePrefabInstance(go, prefabAssetRoot, false, mode);

            foreach (var go in prefabInstanceRoots)
                ReplacePrefabAssetOfPrefabInstance_NoInputValidation(go, prefabAssetRoot, settings, mode);

            EditorUtility.ForceRebuildInspectors();
        }

        public static void ReplacePrefabAssetOfPrefabInstance(GameObject prefabInstanceRoot, GameObject prefabAssetRoot, InteractionMode mode)
        {
            ReplacePrefabAssetOfPrefabInstance(prefabInstanceRoot, prefabAssetRoot, new PrefabReplacingSettings(), mode);
        }

        public static void ReplacePrefabAssetOfPrefabInstance(GameObject prefabInstanceRoot, GameObject prefabAssetRoot, PrefabReplacingSettings settings, InteractionMode mode)
        {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass new PrefabReplacingSettings() as the default when you don't need custom settings.
  2. Use the 3-argument overload ReplacePrefabAssetOfPrefabInstances(roots, asset, mode) which constructs default settings for you.
  3. Null-check settings and substitute a default before calling.

Example fix

// before
PrefabUtility.ReplacePrefabAssetOfPrefabInstances(roots, asset, settings, mode);
// settings could be null

// after
PrefabUtility.ReplacePrefabAssetOfPrefabInstances(roots, asset, settings ?? new PrefabReplacingSettings(), mode);
Defensive patterns

Strategy: validation

Validate before calling

var effectiveSettings = settings ?? new PrefabReplacingSettings();
PrefabUtility.ReplacePrefabAssetOfPrefabInstances(prefabInstanceRoots, prefabAssetRoot, effectiveSettings, mode);

Prevention

When it happens

Trigger: Calling ReplacePrefabAssetOfPrefabInstances(roots, asset, null, mode) — e.g. a settings object that was conditionally created but the condition was false.

Common situations: Serialization/deserialization round-trip that produced null. Optional settings parameter in a wrapper method that wasn't defaulted. Code that intended to call the simpler overload but passed null explicitly.

Related errors


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