Unity-Technologies/UnityCsReference · error · ArgumentNullException
targetPrefab
Error message
targetPrefab
What it means
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.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.bindings.cs:280
[StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
[NativeMethod(ThrowsException = true)]
extern private static GameObject SaveAsPrefabAssetAndConnect_Internal([NotNull] GameObject root, string path, out bool success);
[StaticAccessor("PrefabUtilityBindings", StaticAccessorType.DoubleColon)]
[NativeMethod(ThrowsException = true)]
extern private static GameObject SavePrefabAsset_Internal([NotNull] GameObject root, out bool success);
internal static void AddGameObjectsToPrefabAndConnect(GameObject[] gameObjects, Object targetPrefab)
{
if (gameObjects == null)
throw new ArgumentNullException("gameObjects");
if (gameObjects.Length == 0)
throw new ArgumentException("gameObjects array is empty");
if (targetPrefab == null)
throw new ArgumentNullException("targetPrefab");
if (!PrefabUtility.IsPartOfPrefabAsset(targetPrefab))
throw new ArgumentException("Target Prefab has to be a Prefab Asset");
Object targetPrefabInstance = null;
var targetPrefabObject = PrefabUtility.GetPrefabAssetHandle(targetPrefab);
foreach (GameObject go in gameObjects)
{
if (go == null)
throw new ArgumentException("GameObject in input 'gameObjects' array is null");
if (EditorUtility.IsPersistent(go)) // Prefab asset
throw new ArgumentException("Game object is part of a prefab");
var parentPrefabInstance = GetParentPrefabInstance(go);
if (parentPrefabInstance == null)View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before
var target = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Missing.prefab");
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); // target null
// after
var target = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Enemy.prefab");
if (target == null) throw new System.IO.FileNotFoundException("Target prefab not found.");
PrefabUtility.AddGameObjectsToPrefabAndConnect(gos, target); Defensive patterns
Strategy: validation
Validate before calling
if (targetPrefab == null)
throw new System.IO.FileNotFoundException("Target prefab could not be loaded."); Prevention
- 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.
When it happens
Trigger: Calling AddGameObjectsToPrefabAndConnect(gos, null), or passing a target resolved from a missing/failed AssetDatabase lookup, an unassigned serialized field, or a destroyed object.
Common situations: 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.
Related errors
- gameObjects
- The path is null
- gameObjects array is empty
- Target Prefab has to be a Prefab Asset
- GameObject in input 'gameObjects' array is null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/d823cc35e9e79544.
Report an issue: GitHub.