{"record":{"id":"1ac8e7f40f97a946","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-in-gameobjects-array-is-null","errorCode":null,"errorMessage":"GameObject in gameObjects array is null","messagePattern":"GameObject in gameObjects array is null","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GameObjectUtility.bindings.cs","lineNumber":184,"sourceCode":"        internal static extern bool IsPrefabInstanceHiddenForInContextEditing([NotNull] GameObject go);\n\n        public static GameObject[] DuplicateGameObjects(GameObject[] gameObjects)\n        {\n            return DuplicateGameObjects(gameObjects, true);\n        }\n\n        public static GameObject[] DuplicateGameObjects(GameObject[] gameObjects, bool recordUndo)\n        {\n            if (gameObjects == null)\n                throw new System.ArgumentNullException(\"gameObjects array is null\");\n\n            if (gameObjects.Length == 0)\n                return Array.Empty<GameObject>();\n\n            foreach (GameObject go in gameObjects)\n            {\n                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","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GameObjectUtility.bindings.cs#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Filter nulls out of the array before calling (Where(go => go != null)).","Re-fetch the selection (Selection.gameObjects / Selection.objects) immediately before duplication.","Avoid caching GameObject[] across frames that may destroy objects."],"exampleFix":"// before\nGameObjectUtility.DuplicateGameObjects(selection);\n\n// after\nvar valid = selection.Where(go => go != null).ToArray();\nif (valid.Length == 0) return;\nGameObjectUtility.DuplicateGameObjects(valid);","handlingStrategy":"validation","validationCode":"var valid = gameObjects.Where(go => go != null).ToArray();\nif (valid.Length == 0) return Array.Empty<GameObject>();\nreturn GameObjectUtility.DuplicateGameObjects(valid, recordUndo);","typeGuard":"static GameObject[] NonNullOnly(GameObject[] gos)\n    => gos?.Where(go => go != null).ToArray() ?? Array.Empty<GameObject>();","tryCatchPattern":null,"preventionTips":["Filter destroyed/null objects out of selection arrays before duplication.","Re-fetch Selection.gameObjects immediately before the call.","Avoid caching GameObject[] across destruction events."],"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"}