Unity-Technologies/UnityCsReference · error · ArgumentException
GameObject must be part of a Prefab instance, or null.
Error message
GameObject must be part of a Prefab instance, or null.
What it means
Thrown by PrefabStageUtility.OpenPrefab when openedFromInstance is non-null and PrefabUtility.IsPartOfPrefabInstance returns false. This is the public entry point for opening a prefab stage; the instance object must be a real prefab instance in a scene, or null. This mirrors the same contract enforced later in SetOpenedFromInstanceObject.
Source
Thrown at Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStageUtility.cs:93
internal static PrefabStage OpenPrefab(string prefabAssetPath, GameObject openedFromInstance, StageNavigationManager.Analytics.ChangeType changeTypeAnalytics)
{
var prefabStageMode = openedFromInstance != null ? PrefabStage.Mode.InContext : PrefabStage.Mode.InIsolation;
return OpenPrefab(prefabAssetPath, openedFromInstance, prefabStageMode, changeTypeAnalytics);
}
public static PrefabStage OpenPrefab(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode)
{
return OpenPrefab(prefabAssetPath, openedFromInstance, prefabStageMode, StageNavigationManager.Analytics.ChangeType.EnterViaUnknown);
}
internal static PrefabStage OpenPrefab(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode, StageNavigationManager.Analytics.ChangeType changeTypeAnalytics)
{
if (string.IsNullOrEmpty(prefabAssetPath))
throw new ArgumentNullException(nameof(prefabAssetPath));
if (openedFromInstance != null && !PrefabUtility.IsPartOfPrefabInstance(openedFromInstance))
throw new ArgumentException("GameObject must be part of a Prefab instance, or null.", nameof(openedFromInstance));
if (!prefabAssetPath.EndsWith(".prefab", StringComparison.OrdinalIgnoreCase))
throw new ArgumentException("Incorrect file extension: " + prefabAssetPath + ". Must be '.prefab'", nameof(prefabAssetPath));
if (AssetDatabase.LoadMainAssetAtPath(prefabAssetPath) == null)
throw new ArgumentException("Prefab not found at path " + prefabAssetPath, nameof(prefabAssetPath));
return OpenPrefabMode(prefabAssetPath, openedFromInstance, prefabStageMode, changeTypeAnalytics);
}
static PrefabStage GetExistingPrefabStage(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode)
{
var stageHistory = StageNavigationManager.instance.stageHistory;
for (int i = 1; i < stageHistory.Count; i++)
{
var prefabStage = stageHistory[i] as PrefabStage;
if (prefabStage != null && prefabStage.assetPath == prefabAssetPath)
{View on GitHub (pinned to 225b0fbdb5)
Solutions
- Check PrefabUtility.IsPartOfPrefabInstance(openedFromInstance) before calling, or pass null.
- Source the instance reference from a scene hierarchy, not from AssetDatabase.
- Use the simpler overload that omits openedFromInstance when you have no instance context.
Example fix
// before PrefabStageUtility.OpenPrefab(path, myGo, mode); // after GameObject instance = (myGo != null && PrefabUtility.IsPartOfPrefabInstance(myGo)) ? myGo : null; PrefabStageUtility.OpenPrefab(path, instance, mode);
Defensive patterns
Strategy: validation
Validate before calling
GameObject SafeInstance(GameObject go)
{
return (go != null && PrefabUtility.IsPartOfPrefabInstance(go)) ? go : null;
} Prevention
- Pre-validate openedFromInstance with IsPartOfPrefabInstance before calling OpenPrefab.
- Prefer the overload that omits openedFromInstance when no instance context is needed.
When it happens
Trigger: Calling PrefabStageUtility.OpenPrefab with a scene object that is not a prefab instance. Passing the prefab asset itself rather than an instance. Passing a destroyed or detached object.
Common situations: Editor scripts automating prefab editing pass the wrong reference type. Integrating with custom inspector code that supplies the selected GameObject which may not be a prefab instance.
Related errors
- Prefab Mode: GameObject must be part of a Prefab instance, o
- Prefab Mode: The 'openedFromInstance' GameObject '{go.name}'
- Cannot save as new prefab using the same path
- 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/51a0647981e41d6f.
Report an issue: GitHub.