Unity-Technologies/UnityCsReference · error · ArgumentException

No GameObjects in array.

Error message

No GameObjects in array.

What it means

ApplyAddedGameObjects throws ArgumentException when the array contains zero elements. Applying nothing is not a meaningful operation, so the API rejects the empty array rather than silently no-op. Distinguished from a null array (581) by the non-null-but-empty state.

Source

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

            );

            PrefabUtility.Internal_CallPrefabInstanceApplied(prefabInstanceRoot);
        }

        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)

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check gameObjects.Length > 0 before calling and skip when empty.
  2. Use the singular ApplyAddedGameObject overload only when exactly one object is involved.
  3. Surface 'nothing to apply' as a user message instead of forwarding an empty array.

Example fix

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

// after
if (addedGos != null && addedGos.Length > 0)
    PrefabUtility.ApplyAddedGameObjects(addedGos, path, mode);
Defensive patterns

Strategy: validation

Validate before calling

if (gameObjects == null || gameObjects.Length == 0) return;

Type guard

static bool HasElements(GameObject[] gos) => gos != null && gos.Length > 0;

Prevention

When it happens

Trigger: Calling with new GameObject[0] or Array.Empty<GameObject>(); passing the result of a filtered selection that yielded no added overrides.

Common situations: Selection filtering removed all candidates (none were added overrides) and the empty result was forwarded unchecked; building arrays from user selection that ended up empty.

Related errors


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