Unity-Technologies/UnityCsReference · error · ArgumentException

Input GameObject is null.

Error message

Input GameObject is null.

What it means

Inside the per-element loop of ApplyAddedGameObjects, a null entry in the array is rejected with ArgumentException. Each element must be a live GameObject reference because the method inspects IsAddedGameObjectOverride on each one.

Source

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

        public static void ApplyAddedGameObject(GameObject gameObject, string assetPath, InteractionMode action)
        {
            ApplyAddedGameObjects(new GameObject[] { gameObject}, assetPath, action);
        }

        public static void ApplyAddedGameObjects(GameObject[] gameObjects, string assetPath, InteractionMode action)
        {
            DateTime startTime = DateTime.UtcNow;

            if (gameObjects == null)
                throw new ArgumentNullException(nameof(gameObjects), "Cannot apply added GameObjects. GameObjects array is null.");

            if (gameObjects.Length == 0)
                throw new ArgumentException(nameof(gameObjects), "No GameObjects in array.");

            foreach (GameObject go in gameObjects)
            {
                if (go == null)
                    throw new ArgumentException(nameof(go), "Input GameObject is null.");

                if (!IsAddedGameObjectOverride(go))
                    throw new ArgumentException(nameof(go), $"Cannot apply added GameObject. GameObject '{go.name}' is not an added GameObject override on a Prefab instance.");

                ThrowExceptionIfInstanceIsPersistent(go);
            }

            if (gameObjects.Length > 1 && !HasSameParent(gameObjects))
                throw new ArgumentException(nameof(gameObjects), "ApplyAddedGameObjects requires that GameObjects share the same parent.");

            GameObject gameObject = gameObjects[0];
            Transform instanceParent = gameObject.transform.parent;
            if (instanceParent == null)
                return;

            GameObject prefabSourceGameObjectParent = GetCorrespondingObjectFromSourceAtPath(instanceParent.gameObject, assetPath);
            if (prefabSourceGameObjectParent == null)
                return;

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter nulls before calling: gameObjects = gameObjects.Where(g => g != null).ToArray();
  2. Avoid storing destroyed references; rebuild arrays from current selection right before apply.
  3. Prefer List<GameObject> populated explicitly over fixed-size arrays with leftover nulls.

Example fix

// before
PrefabUtility.ApplyAddedGameObjects(gos, path, mode);

// after
gos = gos.Where(g => g != null).ToArray();
if (gos.Length > 0)
    PrefabUtility.ApplyAddedGameObjects(gos, path, mode);
Defensive patterns

Strategy: validation

Validate before calling

gameObjects = gameObjects?.Where(g => g != null).ToArray();
if (gameObjects == null || gameObjects.Length == 0) return;

Type guard

static bool AllNonNull(GameObject[] gos) => gos != null && gos.All(g => g != null);

Prevention

When it happens

Trigger: Array contains a null element due to a destroyed object, an uninitialized slot, or a failed lookup that slipped into the collection.

Common situations: Collecting GameObjects where some were destroyed between collection and the apply call; arrays built with a fixed size but only partially filled; references cleared by another system mid-frame.

Related errors


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