Unity-Technologies/UnityCsReference · error · ArgumentNullException

gameObject is null

Error message

gameObject is null

What it means

The single-object overload DuplicateGameObject validates its sole argument: a null gameObject causes an immediate ArgumentNullException. This mirrors the array overload's first guard and protects the internal duplicator from null input.

Source

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

                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");

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

            var gameObjects = DuplicateGameObjects_Internal(new[] { gameObject }, recordUndo);

            if (gameObjects.Length != 0)
                return gameObjects[0];
            else
                return null;
        }

        [NativeMethod("DuplicateGameObjects", IsFreeFunction = true)]
        extern private static GameObject[] DuplicateGameObjects_Internal(GameObject[] gameObjects, bool recordUndo);
    }
}

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Null-check the GameObject before calling and early-return.
  2. Use Selection.activeGameObject and guard for null (no selection).
  3. Re-resolve the reference immediately before the call.

Example fix

// before
GameObjectUtility.DuplicateGameObject(Selection.activeGameObject);

// after
var go = Selection.activeGameObject;
if (go == null) return;
GameObjectUtility.DuplicateGameObject(go);
Defensive patterns

Strategy: validation

Validate before calling

if (gameObject == null) return null;
return GameObjectUtility.DuplicateGameObject(gameObject, recordUndo);

Type guard

static bool IsDuplicatableSceneObject(GameObject go)
    => go != null && !EditorUtility.IsPersistent(go);

Prevention

When it happens

Trigger: Calling DuplicateGameObject(null) or DuplicateGameObject(null, recordUndo); passing a destroyed or never-assigned GameObject reference.

Common situations: Editor menu item wired to duplicate the active object when nothing is selected; refactored call site that lost the null check; cached reference that became null after a scene load.

Related errors


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