Unity-Technologies/UnityCsReference · error · ArgumentNullException

GameObject in gameObjects array is null

Error message

GameObject in gameObjects array is null

What it means

After confirming the array is non-null, DuplicateGameObjects iterates each entry and throws ArgumentNullException if any single GameObject is null. This prevents the native duplicator from receiving destroyed/missing references, which would yield undefined behavior or crashes.

Source

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

        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)
        {
            return DuplicateGameObject(gameObject, true);
        }

        public static GameObject DuplicateGameObject(GameObject gameObject, bool recordUndo)
        {
            if (gameObject == null)
                throw new System.ArgumentNullException("gameObject is null");

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Filter nulls out of the array before calling (Where(go => go != null)).
  2. Re-fetch the selection (Selection.gameObjects / Selection.objects) immediately before duplication.
  3. Avoid caching GameObject[] across frames that may destroy objects.

Example fix

// before
GameObjectUtility.DuplicateGameObjects(selection);

// after
var valid = selection.Where(go => go != null).ToArray();
if (valid.Length == 0) return;
GameObjectUtility.DuplicateGameObjects(valid);
Defensive patterns

Strategy: validation

Validate before calling

var valid = gameObjects.Where(go => go != null).ToArray();
if (valid.Length == 0) return Array.Empty<GameObject>();
return GameObjectUtility.DuplicateGameObjects(valid, recordUndo);

Type guard

static GameObject[] NonNullOnly(GameObject[] gos)
    => gos?.Where(go => go != null).ToArray() ?? Array.Empty<GameObject>();

Prevention

When it happens

Trigger: Passing an array that contains a null element (e.g. a destroyed GameObject still referenced); using Selection.gameObjects after some selected objects were destroyed mid-operation; arrays built by concatenation that included gaps.

Common situations: Duplicating a selection that includes an object deleted in the same frame; lazy arrays populated with '??' that left nulls; cross-thread staleness where references became null.

Related errors


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