{"record":{"id":"51a0647981e41d6f","repo":"Unity-Technologies/UnityCsReference","slug":"gameobject-must-be-part-of-a-prefab-instance-or-n","errorCode":null,"errorMessage":"GameObject must be part of a Prefab instance, or null.","messagePattern":"GameObject must be part of a Prefab instance, or null\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStageUtility.cs","lineNumber":93,"sourceCode":"\n        internal static PrefabStage OpenPrefab(string prefabAssetPath, GameObject openedFromInstance, StageNavigationManager.Analytics.ChangeType changeTypeAnalytics)\n        {\n            var prefabStageMode = openedFromInstance != null ? PrefabStage.Mode.InContext : PrefabStage.Mode.InIsolation;\n            return OpenPrefab(prefabAssetPath, openedFromInstance, prefabStageMode, changeTypeAnalytics);\n        }\n\n        public static PrefabStage OpenPrefab(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode)\n        {\n            return OpenPrefab(prefabAssetPath, openedFromInstance, prefabStageMode, StageNavigationManager.Analytics.ChangeType.EnterViaUnknown);\n        }\n\n        internal static PrefabStage OpenPrefab(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode, StageNavigationManager.Analytics.ChangeType changeTypeAnalytics)\n        {\n            if (string.IsNullOrEmpty(prefabAssetPath))\n                throw new ArgumentNullException(nameof(prefabAssetPath));\n\n            if (openedFromInstance != null && !PrefabUtility.IsPartOfPrefabInstance(openedFromInstance))\n                throw new ArgumentException(\"GameObject must be part of a Prefab instance, or null.\", nameof(openedFromInstance));\n\n            if (!prefabAssetPath.EndsWith(\".prefab\", StringComparison.OrdinalIgnoreCase))\n                throw new ArgumentException(\"Incorrect file extension: \" + prefabAssetPath + \". Must be '.prefab'\", nameof(prefabAssetPath));\n\n            if (AssetDatabase.LoadMainAssetAtPath(prefabAssetPath) == null)\n                throw new ArgumentException(\"Prefab not found at path \" + prefabAssetPath, nameof(prefabAssetPath));\n\n            return OpenPrefabMode(prefabAssetPath, openedFromInstance, prefabStageMode, changeTypeAnalytics);\n        }\n\n        static PrefabStage GetExistingPrefabStage(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode)\n        {\n            var stageHistory = StageNavigationManager.instance.stageHistory;\n            for (int i = 1; i < stageHistory.Count; i++)\n            {\n                var prefabStage = stageHistory[i] as PrefabStage;\n                if (prefabStage != null && prefabStage.assetPath == prefabAssetPath)\n                {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStageUtility.cs#L75-L111","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nPrefabStageUtility.OpenPrefab(path, myGo, mode);\n\n// after\nGameObject instance = (myGo != null && PrefabUtility.IsPartOfPrefabInstance(myGo)) ? myGo : null;\nPrefabStageUtility.OpenPrefab(path, instance, mode);","handlingStrategy":"validation","validationCode":"GameObject SafeInstance(GameObject go)\n{\n    return (go != null && PrefabUtility.IsPartOfPrefabInstance(go)) ? go : null;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pre-validate openedFromInstance with IsPartOfPrefabInstance before calling OpenPrefab.","Prefer the overload that omits openedFromInstance when no instance context is needed."],"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"}