Unity-Technologies/UnityCsReference · error · ArgumentException
ApplyPrefabAddedGameObjects requires that GameObjects share
Error message
ApplyPrefabAddedGameObjects requires that GameObjects share the same parent asset path.
What it means
Thrown by ApplyPrefabAddedGameObjects when the supplied GameObjects originate from different prefab asset paths. HasSameAssetPath verifies every ObjectInstanceAndSourcePathInfo entry shares one assetPath, because a single apply call can only target one prefab asset.
Source
Thrown at Editor/Mono/GUI/TargetChoiceHandler.cs:139
EditorUtility.ForceRebuildInspectors();
}
internal static void ApplyPrefabAddedGameObjects(object userData)
{
ObjectInstanceAndSourcePathInfo[] infos = (ObjectInstanceAndSourcePathInfo[])userData;
GameObject[] gameObjects = new GameObject[infos.Length];
for (int i = 0; i < infos.Length; i++)
{
ObjectInstanceAndSourcePathInfo info = infos[i];
gameObjects[i] = info.instanceObject as GameObject;
}
if (!PrefabUtility.HasSameParent(gameObjects))
throw new ArgumentException(nameof(gameObjects), "ApplyPrefabAddedGameObjects requires that GameObjects share the same parent.");
if (!HasSameAssetPath(infos))
throw new ArgumentException(nameof(infos), "ApplyPrefabAddedGameObjects requires that GameObjects share the same parent asset path.");
if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(infos[0].assetPath, PrefabUtility.SaveVerb.Apply))
return;
PrefabUtility.ApplyAddedGameObjects(gameObjects, infos[0].assetPath, InteractionMode.UserAction);
EditorUtility.ForceRebuildInspectors();
}
internal static bool HasSameAssetPath(ObjectInstanceAndSourcePathInfo[] infos)
{
if (infos == null || infos.Length == 0)
throw new ArgumentException(nameof(infos), "Array is invalid.");
string assetPath = infos[0].assetPath;
for (int i = 1; i < infos.Length; i++)
{
if (assetPath != infos[i].assetPath)
return false;View on GitHub (pinned to 225b0fbdb5)
Solutions
- Partition the selection by assetPath and apply each asset group in its own call.
- Verify all entries share infos[0].assetPath before calling the API and prompt the user otherwise.
- Use HasSameAssetPath(infos) as a precondition guard and report the mismatched paths.
Example fix
// before
ApplyPrefabAddedGameObjects(infos); // mixed asset paths
// after
foreach (var group in infos.GroupBy(i => i.assetPath))
ApplyPrefabAddedGameObjects(group.ToArray()); Defensive patterns
Strategy: validation
Validate before calling
bool SameAssetPath(ObjectInstanceAndSourcePathInfo[] infos) => infos.All(i => i.assetPath == infos[0].assetPath);
Prevention
- Partition the selection by assetPath before apply.
- Show the source asset in the Apply confirmation dialog so mismatches are visible.
- Reject cross-asset apply at the UI layer.
When it happens
Trigger: Selecting added GameObjects that come from more than one distinct prefab asset and invoking Apply; passing an infos array whose entries have differing assetPath values.
Common situations: Multi-selection spanning multiple prefab instances (different source assets); a script batching apply across unrelated prefabs for convenience.
Related errors
- ApplyPrefabAddedGameObjects requires that GameObjects share
- Array is invalid.
- The Prefab you want to instantiate is null.
- The source path cannot be empty.
- Path cannot be null or empty.
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/eb80a0795329dabf.
Report an issue: GitHub.