{"record":{"id":"460b0ecd82fc406a","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-mode-the-openedfrominstance-gameobject","errorCode":null,"errorMessage":"Prefab Mode: The 'openedFromInstance' GameObject '{go.name}' is unrelated to the Prefab Asset '{m_PrefabAssetPath}'.","messagePattern":"Prefab Mode: The 'openedFromInstance' GameObject '(.+?)' is unrelated to the Prefab Asset '(.+?)'\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStage.cs","lineNumber":205,"sourceCode":"                    return transform.gameObject;\n\n                transform = transform.parent;\n            }\n\n            return null;\n        }\n\n        void SetOpenedFromInstanceObject(GameObject go)\n        {\n            if (go != null)\n            {\n                if (!PrefabUtility.IsPartOfPrefabInstance(go))\n                    throw new ArgumentException(\"Prefab Mode: GameObject must be part of a Prefab instance, or null.\", nameof(go));\n\n                m_OpenedFromInstanceObject = go;\n                m_OpenedFromInstanceRoot = FindPrefabInstanceRootThatMatchesPrefabAssetPath(go, m_PrefabAssetPath);\n                if (m_OpenedFromInstanceRoot == null)\n                    throw new ArgumentException($\"Prefab Mode: The 'openedFromInstance' GameObject '{go.name}' is unrelated to the Prefab Asset '{m_PrefabAssetPath}'.\");\n\n                m_FileIdForOpenedFromInstanceObject = Unsupported.GetOrGenerateFileIDHint(go);\n            }\n            else\n            {\n                m_OpenedFromInstanceObject = null;\n                m_OpenedFromInstanceRoot = null;\n                m_FileIdForOpenedFromInstanceObject = 0;\n            }\n        }\n\n        void ReconstructInContextStateIfNeeded()\n        {\n            // The previous PrefabStage can have been reloaded if user chose to discard changes when entering this PrefabStage,\n            // which means we need to update our reference to m_OpenedFromInstanceObject to the newly loaded GameObject\n            // (the old GameObject was deleted as part of reloading the PrefabStage).\n            bool needsReconstruction = m_OpenedFromInstanceObject == null && m_FileIdForOpenedFromInstanceObject != 0;\n            if (!needsReconstruction)","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStage.cs#L187-L223","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nstage.SetOpenedFromInstanceObject(selectedGo);\n\n// after\nstring instanceAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(selectedGo);\nif (instanceAssetPath == stage.assetPath)\n    stage.SetOpenedFromInstanceObject(selectedGo);\nelse\n    stage.SetOpenedFromInstanceObject(null);","handlingStrategy":"validation","validationCode":"bool IsInstanceRelatedToPrefab(GameObject go, string prefabAssetPath)\n{\n    if (go == null || !PrefabUtility.IsPartOfPrefabInstance(go)) return false;\n    string instancePath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);\n    return instancePath == prefabAssetPath;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compare PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go) against the stage's assetPath before calling.","When unsure of the relationship, pass null for openedFromInstance."],"tags":["unity","prefab","editor","validation"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}