In this lesson, you will learn how to identify and resolve "Prefab Override Conflicts" that occur when your script’s serialized data clashes with the underlying Prefab Asset’s definitions.
The Conflict: Serialization vs. Prefab Assets
When you modify a value on a Prefab Instance in your scene, Unity marks that property as an "Override." Under the hood, Unity serializes these changes so they persist. However, conflicts arise when you modify data via scripts or external tools before or during serialization, or when the Prefab Asset itself is updated (e.g., via version control).
If your script attempts to change a value that Unity’s serialization system is currently trying to reconcile against the original Prefab Asset, you may end up with a "dirty" state where the inspector shows a blue override bar, but the actual data remains inconsistent.
Diagnosing the Issue
A classic symptom is a property that constantly reverts to its default value or fails to save correctly after a scene reload. This happens because the Prefab Utility and the Serialization System disagree on who owns the current value.
To diagnose this, check if your component is modifying fields that are marked as [SerializeField]. If you are programmatically changing these values in OnValidate() or via a custom tool, you must ensure you are not creating a "Feedback Loop" where the script forces a change, and Unity immediately tries to overwrite it with the stored Prefab override data.
Resolving with Serialization Callbacks
To safely manage these changes, use ISerializationCallbackReceiver. This ensures your logic triggers at the correct point in the data lifecycle, avoiding conflict with the Prefab system:
public class PrefabOverrideManager : MonoBehaviour, ISerializationCallbackReceiver
{
public float myData;
public void OnBeforeSerialize() { /* Perform checks here */ }
public void OnAfterDeserialize()
{
// Use this to sync logic after Unity has finished
// applying Prefab overrides to your instance.
}
}
By placing your logic in OnAfterDeserialize, you ensure that the Prefab’s base values have been fully applied before your script applies its custom overrides, effectively "settling" the conflict before the Inspector renders the data.
Key Takeaway: When prefab overrides conflict with script-driven serialization, use ISerializationCallbackReceiver to defer your data logic until after Unity has finished reconciling the Prefab Asset’s state.