{"record":{"id":"fd953c4fc57282fe","repo":"Unity-Technologies/UnityCsReference","slug":"prefab-mode-gameobject-must-be-part-of-a-prefab-i","errorCode":null,"errorMessage":"Prefab Mode: GameObject must be part of a Prefab instance, or null.","messagePattern":"Prefab Mode: GameObject must be part of a Prefab instance, or null\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStage.cs","lineNumber":200,"sourceCode":"            Transform transform = prefabInstanceObject.transform;\n            while (transform != null)\n            {\n                var assetObject = PrefabUtility.GetCorrespondingObjectFromSourceAtPath(transform, prefabAssetPath);\n                if (assetObject != null && assetObject.parent == null)\n                    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        {","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStage.cs#L182-L218","documentation":"Thrown by PrefabStage.SetOpenedFromInstanceObject when the provided GameObject is non-null but PrefabUtility.IsPartOfPrefabInstance returns false. The method records which prefab instance object the user initiated prefab editing from, so the argument must be an actual instance of a prefab in a scene (or null). A loose asset reference or a plain non-prefab scene object triggers this guard.","triggerScenarios":"Calling PrefabStage API with a GameObject that exists in a scene but is not an instance of any prefab asset. Passing the prefab asset itself (from the Project window) rather than an instance in a scene. Passing a GameObject that was a prefab instance but has been disconnected/ unpacked.","commonSituations":"Programmatically opening a prefab stage via an editor script and accidentally passing a project-asset reference. Script that walks the hierarchy and picks an object that turns out not to be a prefab instance. After unpacking a prefab instance, stale references are passed to the API.","solutions":["Verify PrefabUtility.IsPartOfPrefabInstance(go) is true before calling the API; pass null if you have no instance to associate.","If you only have a project asset, pass null for openedFromInstance.","Ensure the GameObject reference comes from a loaded scene hierarchy, not from AssetDatabase/Project window."],"exampleFix":"// before\nstage.SetOpenedFromInstanceObject(myGameObject);\n\n// after\nif (myGameObject != null && PrefabUtility.IsPartOfPrefabInstance(myGameObject))\n    stage.SetOpenedFromInstanceObject(myGameObject);\nelse\n    stage.SetOpenedFromInstanceObject(null);","handlingStrategy":"validation","validationCode":"bool CanSetOpenedFromInstance(GameObject go)\n{\n    return go == null || PrefabUtility.IsPartOfPrefabInstance(go);\n}","typeGuard":"static bool IsValidPrefabInstanceObject(GameObject go)\n    => go == null || PrefabUtility.IsPartOfPrefabInstance(go);","tryCatchPattern":null,"preventionTips":["Always null-check or IsPartOfPrefabInstance-check before passing a GameObject to PrefabStage instance APIs.","Pass null instead of a non-instance when you only have an asset reference."],"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"}