Unity-Technologies/UnityCsReference · error · ArgumentNullException

gameObjects array is null

Error message

gameObjects array is null

What it means

DuplicateGameObjects validates its input: if the gameObjects array reference itself is null it throws ArgumentNullException immediately, before any iteration. This is the first of three guards (null array -> null element -> persistent asset) protecting the internal duplication routine from invalid input.

Source

Thrown at Editor/Mono/GameObjectUtility.bindings.cs:176

        public static ulong ModifyMaskIfGameObjectIsHiddenForPrefabModeInContext(ulong sceneCullingMask, GameObject gameObject)
        {
            if (!IsPrefabInstanceHiddenForInContextEditing(gameObject))
                return sceneCullingMask;
            return sceneCullingMask & ~SceneManagement.SceneCullingMasks.MainStageExcludingPrefabInstanceObjectsOpenInPrefabMode;
        }

        internal static extern bool IsPrefabInstanceHiddenForInContextEditing([NotNull] GameObject go);

        public static GameObject[] DuplicateGameObjects(GameObject[] gameObjects)
        {
            return DuplicateGameObjects(gameObjects, true);
        }

        public static GameObject[] DuplicateGameObjects(GameObject[] gameObjects, bool recordUndo)
        {
            if (gameObjects == null)
                throw new System.ArgumentNullException("gameObjects array is null");

            if (gameObjects.Length == 0)
                return Array.Empty<GameObject>();

            foreach (GameObject go in gameObjects)
            {
                if (go == null)
                    throw new System.ArgumentNullException("GameObject in gameObjects array is null");

                if (EditorUtility.IsPersistent(go))
                    throw new System.ArgumentException("Duplicating Assets is unsupported by this function. Use AssetDatabase.CopyAsset to duplicate Assets.");
            }

            return DuplicateGameObjects_Internal(gameObjects, recordUndo);
        }

        public static GameObject DuplicateGameObject(GameObject gameObject)
        {

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check the array before calling and skip when empty/null.
  2. Initialize the array from a valid source such as Selection.gameObjects, guarding for null.
  3. Prefer passing Array.Empty<GameObject>() semantically (note the API returns early for length 0 anyway).

Example fix

// before
GameObjectUtility.DuplicateGameObjects(selected);

// after
if (selected == null || selected.Length == 0) return;
GameObjectUtility.DuplicateGameObjects(selected);
Defensive patterns

Strategy: validation

Validate before calling

if (gameObjects == null) return; // or Array.Empty<GameObject>()
GameObjectUtility.DuplicateGameObjects(gameObjects, recordUndo);

Type guard

static bool IsValidGameObjectArray(GameObject[] gos)
    => gos != null && Array.TrueForAll(gos, go => go != null);

Prevention

When it happens

Trigger: Calling GameObjectUtility.DuplicateGameObjects(null) or DuplicateGameObjects(null, recordUndo); passing a field/property that was never initialized; a refactored call site that lost the array construction.

Common situations: Editor script that duplicates a selection but the selection array is null when nothing is selected; threading/timing where the array is set later; copy-paste error omitting array allocation.

Related errors


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