{"record":{"id":"e5bef35c9da1eda8","repo":"Unity-Technologies/UnityCsReference","slug":"gameobjects-array-is-null","errorCode":null,"errorMessage":"gameObjects array is null","messagePattern":"gameObjects array is null","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/GameObjectUtility.bindings.cs","lineNumber":176,"sourceCode":"\n        public static ulong ModifyMaskIfGameObjectIsHiddenForPrefabModeInContext(ulong sceneCullingMask, GameObject gameObject)\n        {\n            if (!IsPrefabInstanceHiddenForInContextEditing(gameObject))\n                return sceneCullingMask;\n            return sceneCullingMask & ~SceneManagement.SceneCullingMasks.MainStageExcludingPrefabInstanceObjectsOpenInPrefabMode;\n        }\n\n        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        {","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/GameObjectUtility.bindings.cs#L158-L194","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Null-check the array before calling and skip when empty/null.","Initialize the array from a valid source such as Selection.gameObjects, guarding for null.","Prefer passing Array.Empty<GameObject>() semantically (note the API returns early for length 0 anyway)."],"exampleFix":"// before\nGameObjectUtility.DuplicateGameObjects(selected);\n\n// after\nif (selected == null || selected.Length == 0) return;\nGameObjectUtility.DuplicateGameObjects(selected);","handlingStrategy":"validation","validationCode":"if (gameObjects == null) return; // or Array.Empty<GameObject>()\nGameObjectUtility.DuplicateGameObjects(gameObjects, recordUndo);","typeGuard":"static bool IsValidGameObjectArray(GameObject[] gos)\n    => gos != null && Array.TrueForAll(gos, go => go != null);","tryCatchPattern":null,"preventionTips":["Null-check arrays from Selection before duplication calls.","Initialize arrays explicitly rather than relying on null.","Guard editor menu handlers for the no-selection case."],"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"}