{"record":{"id":"d823cc35e9e79544","repo":"Unity-Technologies/UnityCsReference","slug":"targetprefab","errorCode":null,"errorMessage":"targetPrefab","messagePattern":"targetPrefab","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.bindings.cs","lineNumber":280,"sourceCode":"\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\");\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)","sourceCodeStart":262,"sourceCodeEnd":298,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.bindings.cs#L262-L298","documentation":"Thrown by AddGameObjectsToPrefabAndConnect when `targetPrefab` is null. The target defines which Prefab Asset the scene objects will be connected into, so a null target is an ArgumentNullException caught before iterating gameObjects.","triggerScenarios":"Calling AddGameObjectsToPrefabAndConnect(gos, null), or passing a target resolved from a missing/failed AssetDatabase lookup, an unassigned serialized field, or a destroyed object.","commonSituations":"targetPrefab loaded via AssetDatabase.LoadAssetAtPath with a wrong path (returns null). Serialized reference to a prefab that was deleted or not yet imported. Drag-and-drop target left unassigned in an editor window.","solutions":["Assign a valid Prefab Asset reference (GameObject loaded from Assets).","Validate the load result is non-null after AssetDatabase.LoadAssetAtPath.","Check `targetPrefab != null` before calling and report a clear error if missing."],"exampleFix":"// before\nvar target = AssetDatabase.LoadAssetAtPath<GameObject>(\"Assets/Missing.prefab\");\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // target null\n\n// after\nvar target = AssetDatabase.LoadAssetAtPath<GameObject>(\"Assets/Prefabs/Enemy.prefab\");\nif (target == null) throw new System.IO.FileNotFoundException(\"Target prefab not found.\");\nPrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target);","handlingStrategy":"validation","validationCode":"if (targetPrefab == null)\n    throw new System.IO.FileNotFoundException(\"Target prefab could not be loaded.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check the return of AssetDatabase.LoadAssetAtPath for null.","Null-check the target before calling and surface a clear 'asset missing' error.","Cache prefab references via the Editor rather than re-resolving from possibly-wrong paths."],"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"}