Unity-Technologies/UnityCsReference · error · ArgumentNullException

instanceRoots

Error message

instanceRoots

What it means

Thrown by ApplyPrefabInstances(GameObject[] instanceRoots, InteractionMode) when the array argument is null. The method iterates the array immediately to validate each root, so a null array dereferences before any work begins. Unity uses ArgumentNullException with nameof(instanceRoots) to make the offending parameter explicit.

Source

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

                if (undoFlushTrackedObjects && action == InteractionMode.UserAction)
                    Undo.FlushTrackedObjects();
            }

            Analytics.SendApplyEvent(
                Analytics.ApplyScope.EntirePrefab,
                instanceRoot,
                null,
                action,
                startTime,
                false
            );
        }

        public static void ApplyPrefabInstances(GameObject[] instanceRoots, InteractionMode action)
        {
            if (instanceRoots == null)
                throw new ArgumentNullException(nameof(instanceRoots));

            foreach (var instanceRoot in instanceRoots)
            {
                ThrowExceptionIfNotValidPrefabInstanceObject(instanceRoot, true);
            }

            // Apply sequentially but import after all input have been saved to disk
            AssetDatabase.StartAssetEditing();
            try
            {
                foreach (var instanceRoot in instanceRoots)
                {
                    ApplyPrefabInstance(instanceRoot, action, undoFlushTrackedObjects:false);
                }
            }
            finally
            {
                AssetDatabase.StopAssetEditing();

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Pass an empty array instead of null: ApplyPrefabInstances(Array.Empty<GameObject>(), action).
  2. Initialize the array before conditional population and null-check before the call.
  3. If the array comes from a query, coalesce with ?? Array.Empty<GameObject>().

Example fix

// before
var roots = Selection.transforms.Length > 0 ? Selection.gameObjects : null;
PrefabUtility.ApplyPrefabInstances(roots, InteractionMode.AutomatedAction);

// after
var roots = Selection.gameObjects ?? Array.Empty<GameObject>();
PrefabUtility.ApplyPrefabInstances(roots, InteractionMode.AutomatedAction);
Defensive patterns

Strategy: validation

Validate before calling

instanceRoots ??= Array.Empty<GameObject>();
if (instanceRoots.Length == 0) return;
PrefabUtility.ApplyPrefabInstances(instanceRoots, action);

Type guard

static bool IsUsableRootArray(GameObject[] arr)
    => arr != null && arr.All(go => go != null && PrefabUtility.IsPartOfPrefabInstance(go));

Prevention

When it happens

Trigger: Calling PrefabUtility.ApplyPrefabInstances(null, action); passing a variable that was never assigned; passing the result of a LINQ/Find operation that returned null because no selection existed.

Common situations: Selection.transforms-derived GameObject arrays where nothing is selected; editor scripts that build the array conditionally and leave it null when no instances match; refactors that removed the array population step.

Related errors


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