Today I learned
Why do Unity prefab variant overrides vanish on rebuild?
· updated #Unity
Variant overrides are tied to each base object's file ID, not its name. If an editor script calls PrefabUtility.SaveAsPrefabAsset(fresh, path) over the old base, objects from an imported model keep their IDs, but objects the script created (new GameObject, AddComponent) get new ones, and the variant's edits on them are silently orphaned.
Fix: update the base in place instead of overwriting it.
GameObject old = PrefabUtility.LoadPrefabContents(path);
// match fresh -> old by name path (+ index among same-named siblings),
// components by type and order; add missing, destroy gone ones
EditorUtility.CopySerialized(freshComp, oldComp);
// then repoint ObjectReference fields from `fresh` objects to their `old` matches
PrefabUtility.SaveAsPrefabAsset(old, path);
PrefabUtility.UnloadPrefabContents(old);
Treat a nested prefab instance as one unit (same source prefab = same object), or you rebuild its insides.
Hit this making a generated train car hand-editable as a variant: test edits survived two rebuilds only after the in-place sync.