Unity-Technologies/UnityCsReference · error · ArgumentNullException
Prefab source must not be null.
Error message
Prefab source must not be null.
What it means
Thrown by ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode) when assetGameObject is null — ArgumentNullException(nameof(assetGameObject)). The asset GameObject identifies which child was removed in the instance; without it Unity cannot map the removal back to the source hierarchy.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:1566
}
private static void RemoveRemovedGameObjectOverridesWhichAreNull(Object prefabInstanceObject)
{
var removedGameObjects = PrefabUtility.GetRemovedGameObjects(prefabInstanceObject);
#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.
var filteredRemovedGameObjects = (from go in removedGameObjects where go != null select go).ToArray();
#pragma warning restore UA2001
PrefabUtility.SetRemovedGameObjects(prefabInstanceObject, filteredRemovedGameObjects);
}
public static void ApplyRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action)
{
DateTime startTime = DateTime.UtcNow;
ThrowExceptionIfNotValidPrefabInstanceObject(gameObjectInInstance, true);
if (assetGameObject == null)
throw new ArgumentNullException(nameof(assetGameObject), "Prefab source must not be null.");
if (!IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)))
throw new ArgumentException("Prefab instance must match Prefab source.");
if (assetGameObject.transform.root == assetGameObject.transform)
throw new ArgumentException("The asset GameObject cannot be the root as the root cannot be removed as an override.");
var actionName = "Apply Prefab removed GameObject";
var prefabInstanceObject = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);
GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);
if (prefabInstanceObject == null)
throw new ArgumentNullException(nameof(prefabInstanceObject), "Prefab instance must not be null.");
PrefabUtility.Internal_CallPrefabInstanceApplying(prefabInstanceRoot);
string assetPath = AssetDatabase.GetAssetPath(assetGameObject);
GameObject assetRoot = GetRootGameObject(assetGameObject);
byte[] originalFileContent = null;
View on GitHub (pinned to 225b0fbdb5)
Solutions
- Null-check assetGameObject before calling: if (assetGameObject == null) return;
- Resolve assetGameObject from a freshly loaded prefab asset before the call.
- Confirm gameObjectInInstance is itself a valid prefab instance object (ThrowExceptionIfNotValidPrefabInstanceObject runs first).
Example fix
// before PrefabUtility.ApplyRemovedGameObject(instanceGo, assetGo, InteractionMode.UserAction); // after if (assetGo == null) return; PrefabUtility.ApplyRemovedGameObject(instanceGo, assetGo, InteractionMode.UserAction);
Defensive patterns
Strategy: validation
Validate before calling
if (assetGameObject == null) { Debug.LogWarning("Prefab source GameObject is null."); return; }
PrefabUtility.ApplyRemovedGameObject(gameObjectInInstance, assetGameObject, action); Type guard
static bool IsLiveAssetGameObject(GameObject assetGo)
=> assetGo != null && EditorUtility.IsPersistent(assetGo); Prevention
- Resolve the asset GameObject from a freshly loaded prefab asset.
- Pass the asset-side GameObject, not the instance one.
- Null-check assetGameObject before calling.
When it happens
Trigger: Calling ApplyRemovedGameObject with a null assetGameObject; passing a destroyed GameObject reference; forwarding a Find/GetComponent lookup that returned null.
Common situations: Tooling applying 'removed GameObject' overrides where the asset-side lookup failed; stale references after the asset hierarchy changed; passing the instance GameObject where the asset GameObject was expected.
Related errors
- instanceRoots
- prefabInstances
- Cannot apply added component. Component is null.
- Cannot revert added component. Component is null.
- Prefab source may not be null.
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/5aed0f32e604f5c1.
Report an issue: GitHub.