Unity-Technologies/UnityCsReference · error · ArgumentException
Prefab instance should match Prefab source.
Error message
Prefab instance should match Prefab source.
What it means
RevertRemovedGameObject verifies that gameObjectInInstance corresponds to assetGameObject's prefab handle via IsPrefabInstanceObjectOf. If the two belong to different prefabs (or one is not actually an instance object of the other's asset), the pairing is invalid and an ArgumentException is thrown.
Source
Thrown at Editor/Mono/Prefabs/PrefabUtility.cs:1781
}
if (index != -1)
{
#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 != removedGameObjects[index] select go).ToArray();
#pragma warning restore UA2001
PrefabUtility.SetRemovedGameObjects(instanceObject, filteredRemovedGameObjects);
}
}
public static void RevertRemovedGameObject(GameObject gameObjectInInstance, GameObject assetGameObject, InteractionMode action)
{
ThrowExceptionIfNotValidPrefabInstanceObject(gameObjectInInstance, false);
if (assetGameObject == null)
throw new ArgumentNullException(nameof(assetGameObject), "Prefab source may not be null.");
if (!IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGameObject)))
throw new ArgumentException("Prefab instance should 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 = "Revert Prefab removed GameObject";
var prefabInstanceHandle = PrefabUtility.GetPrefabInstanceHandle(gameObjectInInstance);
GameObject prefabInstanceRoot = GetOutermostPrefabInstanceRoot(gameObjectInInstance);
if (action == InteractionMode.UserAction)
Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, actionName);
RemoveRemovedGameObjectOverride(prefabInstanceHandle, assetGameObject);
}
public static void RevertAddedGameObject(GameObject gameObject, InteractionMode action)
{
if (gameObject == null)
throw new ArgumentNullException(nameof(gameObject), "Cannot revert added GameObject. GameObject is null.");View on GitHub (pinned to 225b0fbdb5)
Solutions
- Pair arguments using GetCorrespondingObjectFromSource so both refer to the same prefab asset.
- Ensure both arguments were obtained from the same prefab asset handle (GetPrefabAssetHandle matches).
- Rebuild the instance/source mapping from the current prefab state before reverting.
Example fix
// before
PrefabUtility.RevertRemovedGameObject(goInInstance, assetGo, mode);
// after
var assetGo = PrefabUtility.GetCorrespondingObjectFromSource(goInInstance);
if (assetGo != null && PrefabUtility.IsPrefabInstanceObjectOf(goInInstance, PrefabUtility.GetPrefabAssetHandle(assetGo)))
PrefabUtility.RevertRemovedGameObject(goInInstance, assetGo, mode); Defensive patterns
Strategy: validation
Validate before calling
var assetGo = PrefabUtility.GetCorrespondingObjectFromSource(gameObjectInInstance); if (assetGo == null) return; if (!PrefabUtility.IsPrefabInstanceObjectOf(gameObjectInInstance, PrefabUtility.GetPrefabAssetHandle(assetGo))) return;
Type guard
static bool InstanceMatchesSource(GameObject instanceGo, GameObject assetGo) =>
assetGo != null && PrefabUtility.IsPrefabInstanceObjectOf(instanceGo,
PrefabUtility.GetPrefabAssetHandle(assetGo)); Prevention
- Derive both arguments from the same prefab asset so the pairing is guaranteed.
- Use GetCorrespondingObjectFromSource to obtain the matching asset object for a given instance object.
- Rebuild the instance/source mapping from current prefab state before reverting across multiple prefabs.
When it happens
Trigger: Passing a gameObjectInInstance and assetGameObject that originate from different prefab assets, or where the instance object no longer maps to the supplied source.
Common situations: Iterating removed overrides across multiple prefabs and cross-pairing instance objects with the wrong asset objects; prefab asset swapped/reimported between capture and revert.
Related errors
- guid
- prefabInstance
- Provided GameObject is not a Prefab instance
- The inputObject is null
- Given input object is not a prefab asset
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/fde8068babe2055a.
Report an issue: GitHub.