{"record":{"id":"2e168fa68a1db5a8","repo":"Unity-Technologies/UnityCsReference","slug":"gameobjects","errorCode":null,"errorMessage":"gameObjects","messagePattern":"gameObjects","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":274,"sourceCode":"        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        extern private static GameObject ApplyPrefabInstance_Internal([NotNull] GameObject root);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SaveAsPrefabAsset_Internal([NotNull] GameObject root, string path, out bool success);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SaveAsPrefabAssetAndConnect_Internal([NotNull] GameObject root, string path, out bool success);\n\n        [StaticAccessor(\"PrefabUtilityBindings\", StaticAccessorType.DoubleColon)]\n        [NativeMethod(ThrowsException = true)]\n        extern private static GameObject SavePrefabAsset_Internal([NotNull] GameObject root, out bool success);\n\n        internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)\n        {\n            if (gameObjects == null)\n                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\");","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L256-L292","documentation":"Thrown by AddGameObjectsToPrefabAndConnect when the `gameObjects` array argument is null. The method connects existing scene GameObjects into a target Prefab, so a null array is a programmer error caught as ArgumentNullException before any work begins.","triggerScenarios":"Calling AddGameObjectsToPrefabAndConnect(null, targetPrefab), or passing a field/variable that was never populated (e.g. a Selection.transforms array that returned nothing and was assigned null).","commonSituations":"Editor tools driven by selection where nothing is selected. Arrays built via LINQ that can be null instead of empty. Refactors that removed the array population step.","solutions":["Pass a non-null GameObject[] even if empty (though empty throws separately).","Initialize arrays to empty before populating: new GameObject[0].","Guard the selection: if (gameObjects == null) return; or inform the user to select objects."],"exampleFix":"// before\nPrefabUtility.AddGameObjectsToPrefabAndConnect(null, target);\n\n// after\nGameObject[] gos = Selection.gameObjects ?? Array.Empty<GameObject>();\nif (gos.Length == 0) return;\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"if (gameObjects == null)\n    gameObjects = System.Array.Empty<GameObject>();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize arrays/lists before populating rather than leaving them null.","Coalesce selection results with `?? Array.Empty<T>()`.","Guard null arrays upstream in your editor tool's entry points."],"tags":["prefab","argument-null","add-to-prefab"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}