{"record":{"id":"581505e71a634580","repo":"Unity-Technologies/UnityCsReference","slug":"mismatching-objects-cannot-apply-serializereferen","errorCode":null,"errorMessage":"Mismatching Objects: Cannot apply SerializeReference field since the Prefab instance is referencing a new object compared to the Prefab Asset.\n\nThis means that the changes from the Prefab Asset cannot be merged back to the Prefab instance.\n\nYou can apply the root of the field or the entire component(instance property {instanceProperty.managedReferencePropertyPath}, asset property {sourceProperty.managedReferencePropertyPath})","messagePattern":"Mismatching Objects: Cannot apply SerializeReference field since the Prefab instance is referencing a new object compared to the Prefab Asset\\.\n\nThis means that the changes from the Prefab Asset cannot be merged back to the Prefab instance\\.\n\nYou can apply the root of the field or the entire component\\(instance property (.+?), asset property (.+?)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Prefabs/PrefabUtility.cs","lineNumber":1005,"sourceCode":"        }\n\n        static bool WarnIfApplyingManagedReferenceFieldIsNotPossible(SerializedProperty instanceProperty, SerializedProperty sourceProperty, InteractionMode action)\n        {\n            bool isReferencingAManagedReferenceField = instanceProperty.isReferencingAManagedReferenceField;\n            if (!isReferencingAManagedReferenceField)\n                return false;\n\n            if (sourceProperty != null)\n            {\n                // Even if we find the propertyPath on the source object we also need to validate if this is a managed reference to the correct source object, except when\n                // applying an entire object because then entire state is transfered to the asset and can merged back to the instance so here we don't show the dialog.\n                bool applyingNotPossible = instanceProperty.managedReferencePropertyPath != sourceProperty.managedReferencePropertyPath;\n                if (applyingNotPossible)\n                {\n                    string title = L10n.Tr(\"Mismatching Objects\");\n                    string errorMsg = L10n.Tr(\"Cannot apply SerializeReference field since the Prefab instance is referencing a new object compared to the Prefab Asset.\\n\\nThis means that the changes from the Prefab Asset cannot be merged back to the Prefab instance.\\n\\nYou can apply the root of the field or the entire component\");\n                    if (action == InteractionMode.AutomatedAction)\n                        throw new InvalidOperationException(title + \": \" + errorMsg + $\"(instance property { instanceProperty.managedReferencePropertyPath}, asset property { sourceProperty.managedReferencePropertyPath})\");\n                    else\n                        EditorUtility.DisplayDialog(\n                            title,\n                            errorMsg,\n                            L10n.Tr(\"OK\"));\n                    return true;\n                }\n            }\n            else\n            {\n                // Special handling for managed references.\n                // If we have a valid managedReferencePropertyPath then the types must be different since we could not find the\n                // propertyPath on the Prefab Asset, tell the user that this is not supported.\n                if (!string.IsNullOrEmpty(instanceProperty.managedReferencePropertyPath))\n                {\n                    string title = L10n.Tr(\"Mismatching Types\");\n                    string errorMsg = L10n.Tr(\"Cannot apply a SerializeReference sub field when the type from the Prefab instance is different from the Prefab Asset.\\n\\nYou can apply the root of the field or the entire component\");\n                    if (action == InteractionMode.AutomatedAction)","sourceCodeStart":987,"sourceCodeEnd":1023,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Prefabs/PrefabUtility.cs#L987-L1023","documentation":"Thrown (AutomatedAction) or shown as a dialog (UserAction) when applying a SerializeReference sub-field whose managedReferencePropertyPath on the instance differs from the asset. The instance is referencing a different managed object than the asset, so Unity cannot merge the asset's state back — only the whole field or whole component can be applied. The exception interpolates both property paths for diagnostics.","triggerScenarios":"Selecting a nested SerializeReference sub-field on a prefab instance whose polymorphic object was swapped (different managed reference identity) relative to the prefab asset, then invoking Apply override via automation.","commonSituations":"Polymorphic [SerializeReference] fields where the instance assigned a different subtype instance than the asset; inventory/skill systems using managed references; applying a single nested property after a reference swap.","solutions":["Apply the entire SerializeReference root field instead of a sub-field (use the property's root path).","Apply the whole component override rather than a single nested reference property.","Before applying, check instanceProperty.managedReferencePropertyPath == sourceProperty.managedReferencePropertyPath and only apply when they match."],"exampleFix":"// before\nPrefabUtility.ApplyPropertyOverride(instanceRoot, \"myField.nested.subValue\", assetPath);\n\n// after\n// apply at the SerializeReference root instead\nPrefabUtility.ApplyPropertyOverride(instanceRoot, \"myField\", assetPath);","handlingStrategy":"validation","validationCode":"// Compare managed reference identity before applying a sub-field\nvar inst = new SerializedObject(instanceObj).FindProperty(path);\nvar src = new SerializedObject(assetObj).FindProperty(path);\nif (inst != null && src != null &&\n    inst.managedReferencePropertyPath == src.managedReferencePropertyPath)\n    PrefabUtility.ApplyPropertyOverride(instanceRoot, path, assetPath);","typeGuard":"static bool CanApplySubField(SerializedProperty inst, SerializedProperty src)\n    => inst != null && src != null &&\n       inst.managedReferencePropertyPath == src.managedReferencePropertyPath;","tryCatchPattern":"try { PrefabUtility.ApplyPropertyOverride(root, subPath, assetPath); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Mismatching Objects\"))\n{ PrefabUtility.ApplyPropertyOverride(root, refRootPath, assetPath); }","preventionTips":["Apply at the SerializeReference root field, not a sub-field, when the reference object differs.","Compare managedReferencePropertyPath between instance and asset before sub-field apply.","Treat polymorphic managed references as apply-at-root by default."],"tags":["prefab","serializereference","override","polymorphism"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}