Unity-Technologies/UnityCsReference · error · ArgumentException
Prefab Mode: The 'openedFromInstance' GameObject '{go.name}'
Error message
Prefab Mode: The 'openedFromInstance' GameObject '{go.name}' is unrelated to the Prefab Asset '{m_PrefabAssetPath}'. What it means
Thrown by SetOpenedFromInstanceObject after FindPrefabInstanceRootThatMatchesPrefabAssetPath returns null, meaning the passed GameObject's prefab instance root does not correspond to the prefab asset currently being edited (m_PrefabAssetPath). This catches cases where the instance object is a valid prefab instance but belongs to a different prefab asset than the one being opened in this PrefabStage.
Source
Thrown at Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStage.cs:205
return transform.gameObject;
transform = transform.parent;
}
return null;
}
void SetOpenedFromInstanceObject(GameObject go)
{
if (go != null)
{
if (!PrefabUtility.IsPartOfPrefabInstance(go))
throw new ArgumentException("Prefab Mode: GameObject must be part of a Prefab instance, or null.", nameof(go));
m_OpenedFromInstanceObject = go;
m_OpenedFromInstanceRoot = FindPrefabInstanceRootThatMatchesPrefabAssetPath(go, m_PrefabAssetPath);
if (m_OpenedFromInstanceRoot == null)
throw new ArgumentException($"Prefab Mode: The 'openedFromInstance' GameObject '{go.name}' is unrelated to the Prefab Asset '{m_PrefabAssetPath}'.");
m_FileIdForOpenedFromInstanceObject = Unsupported.GetOrGenerateFileIDHint(go);
}
else
{
m_OpenedFromInstanceObject = null;
m_OpenedFromInstanceRoot = null;
m_FileIdForOpenedFromInstanceObject = 0;
}
}
void ReconstructInContextStateIfNeeded()
{
// The previous PrefabStage can have been reloaded if user chose to discard changes when entering this PrefabStage,
// which means we need to update our reference to m_OpenedFromInstanceObject to the newly loaded GameObject
// (the old GameObject was deleted as part of reloading the PrefabStage).
bool needsReconstruction = m_OpenedFromInstanceObject == null && m_FileIdForOpenedFromInstanceObject != 0;
if (!needsReconstruction)View on GitHub (pinned to 225b0fbdb5)
Solutions
- Confirm the instance object's prefab asset path matches the PrefabStage's prefabAssetPath before calling.
- Use PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go) and compare it to the stage's asset path.
- Pass null for openedFromInstance when you cannot guarantee the relationship.
Example fix
// before
stage.SetOpenedFromInstanceObject(selectedGo);
// after
string instanceAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(selectedGo);
if (instanceAssetPath == stage.assetPath)
stage.SetOpenedFromInstanceObject(selectedGo);
else
stage.SetOpenedFromInstanceObject(null); Defensive patterns
Strategy: validation
Validate before calling
bool IsInstanceRelatedToPrefab(GameObject go, string prefabAssetPath)
{
if (go == null || !PrefabUtility.IsPartOfPrefabInstance(go)) return false;
string instancePath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);
return instancePath == prefabAssetPath;
} Prevention
- Compare PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go) against the stage's assetPath before calling.
- When unsure of the relationship, pass null for openedFromInstance.
When it happens
Trigger: Passing a prefab instance object whose root prefab differs from the prefab asset path of the stage being opened. Cross-referencing an instance from prefab A while opening prefab B in the editor. Variants where the instance root resolves to a different asset path.
Common situations: Editor script grabs the current selection (a prefab instance of X) and tries to open prefab Y's stage with it. Prefab variants causing path mismatch. Multiple prefab assets sharing similar structures leading to confusion in automated tooling.
Related errors
- Prefab Mode: GameObject must be part of a Prefab instance, o
- Cannot save as new prefab using the same path
- GameObject must be part of a Prefab instance, or null.
- Incorrect file extension: {prefabAssetPath}. Must be '.prefa
- Prefab not found at path {prefabAssetPath}
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/460b0ecd82fc406a.
Report an issue: GitHub.