{"record":{"id":"bd7e93ea5e9b60ac","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-in-input-gameobjects-array-is-null","errorCode":null,"errorMessage":"GameObject in input 'gameObjects' array is null","messagePattern":"GameObject in input 'gameObjects' array is null","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":292,"sourceCode":"                throw new ArgumentNullException(\"gameObjects\");\n\n            if (gameObjects.Length == 0)\n                throw new ArgumentException(\"gameObjects array is empty\");\n\n            if (targetPrefab == null)\n                throw new ArgumentNullException(\"targetPrefab\");\n\n            if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))\n                throw new ArgumentException(\"Target Prefab has to be a Prefab Asset\");\n\n            Object targetPrefabInstance = null;\n\n            var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);\n\n            foreach (GameObject go in gameObjects)\n            {\n                if (go == null)\n                    throw new ArgumentException(\"GameObject in input 'gameObjects' array is null\");\n\n                if (EditorUtility.IsPersistent(go))  // Prefab asset\n                    throw new ArgumentException(\"Game object is part of a prefab\");\n\n                var parentPrefabInstance = GetParentPrefabInstance(go);\n                if (parentPrefabInstance == null)\n                    throw new ArgumentException(\"GameObject is not (directly) parented under a target Prefab instance.\");\n\n                if (targetPrefabInstance == null)\n                {\n                    targetPrefabInstance = parentPrefabInstance;\n                    if (!IsPrefabInstanceObjectOf(go.transform.parent, targetPrefabObject))\n                        throw new ArgumentException(\"GameObject is not parented under a target Prefab instance.\");\n                }\n                else\n                {\n                    if (parentPrefabInstance != targetPrefabInstance)\n                    {","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L274-L310","documentation":"Thrown inside the AddGameObjectsToPrefabAndConnect loop when an individual element `go` in the gameObjects array is null. Each array element is null-checked before any prefab logic runs on it, so a single null entry aborts the whole operation.","triggerScenarios":"An array containing a null slot, e.g. from Selection.transforms converted to GameObjects where a transform was missing, a List with Add(null), or an array sized larger than its filled entries.","commonSituations":"Building the array from destroyed objects, Find calls returning null, mixed valid/invalid selection, or LINQ that preserves nulls. Arrays pre-allocated with `new GameObject[n]` then partially filled.","solutions":["Filter nulls before calling: gos = gos.Where(g => g != null).ToArray().","Ensure no destroyed/null references are added to the list.","Validate each element in a loop and report which index was null for debugging."],"exampleFix":"// before\nGameObject[] gos = Selection.gameObjects; // may contain nulls from editor state\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);\n\n// after\nGameObject[] gos = Selection.gameObjects.Where(g => g != null).ToArray();\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"gameObjects = gameObjects.Where(g => g != null).ToArray();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Filter nulls out of any array before passing it to bulk prefab APIs.","Avoid pre-allocating arrays larger than needed; use List<T> and ToArray().","Log which index was null during selection building to catch upstream bugs."],"tags":["prefab","add-to-prefab","null-element"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}