Understanding Unity Prefab Overrides and Asset Serialization
One of the most infuriating occurrences for indie developers is spending hours tweaking weapon fire rates, collider offsets, or script variables on a hierarchy GameObject, only to find all changes mysteriously discarded upon reloading the scene or restarting Unity. This issue arises from misunderstanding how Unity separates Prefab Asset files (on disk in the Project window) from Prefab Instances (in an open scene hierarchy).
Why Hierarchy Edits Revert Unexpectedly
- Changes Made in Play Mode: If you modify inspector values while the Unity Play button is active, Unity will reset all GameObject parameters back to their pre-play state the moment Play Mode is stopped. Always exit Play Mode or copy the component before stopping to preserve numbers.
- Unapplied Prefab Overrides: When you modify a prefab in the scene hierarchy, Unity highlights the modified fields with a vertical blue line. These changes exist purely as local instance overrides. If you delete the instance or re-instantiate from the project asset, those changes will vanish unless you click Overrides → Apply All.
- Prefab Mode vs Scene Isolation: Double-clicking the prefab asset opens Prefab Isolation Mode. Edits made in Prefab Mode automatically write directly to disk on
.prefabassets, permanently updating all instances across all scenes.
How to Guarantee Prefab Persistence
- Select the modified GameObject in the scene hierarchy.
- In the top right of the Inspector, click the Overrides dropdown button.
- Review the modified property list to ensure unintended changes are not included.
- Click Apply All to commit changes back to the source
.prefabasset on disk. - Press Ctrl + S (Cmd + S on Mac) to save both the scene and project asset database.
EditorPrefabSaver.cs (Editor Scripting)
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public static class EditorPrefabSaver
{
// Menu shortcut to forcefully commit selected hierarchy prefab overrides to disk
[MenuItem("Khawajja Tools/Apply Selected Prefab Overrides %#&s")]
public static void ApplyOverrides()
{
GameObject selected = Selection.activeGameObject;
if (selected != null && PrefabUtility.IsPartOfPrefabInstance(selected))
{
PrefabUtility.ApplyPrefabInstance(selected, InteractionMode.UserAction);
AssetDatabase.SaveAssets();
Debug.Log($"<color=green>[Prefab Saved]</color> Successfully applied overrides for '{selected.name}' to disk!");
}
}
}
#endif