{"record":{"id":"fba0108170d3cc17","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-is-null","errorCode":null,"errorMessage":"gameObject is null","messagePattern":"gameObject is null","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GameObjectUtility.bindings.cs","lineNumber":201,"sourceCode":"                if (go == null)\n                    throw new System.ArgumentNullException(\"GameObject in gameObjects array is null\");\n\n                if (EditorUtility.IsPersistent(go))\n                    throw new System.ArgumentException(\"Duplicating Assets is unsupported by this function. Use AssetDatabase.CopyAsset to duplicate Assets.\");\n            }\n\n            return DuplicateGameObjects_Internal(gameObjects, recordUndo);\n        }\n\n        public static GameObject DuplicateGameObject(GameObject gameObject)\n        {\n            return DuplicateGameObject(gameObject, true);\n        }\n\n        public static GameObject DuplicateGameObject(GameObject gameObject, bool recordUndo)\n        {\n            if (gameObject == null)\n                throw new System.ArgumentNullException(\"gameObject is null\");\n\n            if (EditorUtility.IsPersistent(gameObject))\n                throw new System.ArgumentException(\"Duplicating Assets is unsupported by this function. Use AssetDatabase.CopyAsset to duplicate Assets.\");\n\n            var gameObjects = DuplicateGameObjects_Internal(new[] { gameObject }, recordUndo);\n\n            if (gameObjects.Length != 0)\n                return gameObjects[0];\n            else\n                return null;\n        }\n\n        [NativeMethod(\"DuplicateGameObjects\", IsFreeFunction = true)]\n        extern private static GameObject[] DuplicateGameObjects_Internal(GameObject[] gameObjects, bool recordUndo);\n    }\n}\n","sourceCodeStart":183,"sourceCodeEnd":218,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GameObjectUtility.bindings.cs#L183-L218","documentation":"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.","triggerScenarios":"Calling DuplicateGameObject(null) or DuplicateGameObject(null, recordUndo); passing a destroyed or never-assigned GameObject reference.","commonSituations":"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.","solutions":["Null-check the GameObject before calling and early-return.","Use Selection.activeGameObject and guard for null (no selection).","Re-resolve the reference immediately before the call."],"exampleFix":"// before\nGameObjectUtility.DuplicateGameObject(Selection.activeGameObject);\n\n// after\nvar go = Selection.activeGameObject;\nif (go == null) return;\nGameObjectUtility.DuplicateGameObject(go);","handlingStrategy":"validation","validationCode":"if (gameObject == null) return null;\nreturn GameObjectUtility.DuplicateGameObject(gameObject, recordUndo);","typeGuard":"static bool IsDuplicatableSceneObject(GameObject go)\n    => go != null && !EditorUtility.IsPersistent(go);","tryCatchPattern":null,"preventionTips":["Null-check Selection.activeGameObject before duplicating.","Guard editor menu items against no-selection.","Re-resolve GameObject references immediately before use."],"tags":["gameobject","null-argument","editor-scripting","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}