Skip to content

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.

csharp
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.

Docs: PrefabUtility.LoadPrefabContents

The 30-second version

Share LinkedIn Post on X

Related notes

Newsletter

Occasional notes on engineering, game tech, and new tools — no spam, unsubscribe anytime.

Ravy

Engineering manager, founder, and software developer building web platforms, game systems, real-time services, and automation tools. 15 years of shipped work — currently at Gaijin.net.

Get in Touch

Building an MVP, shipping a game feature, or automating a team workflow? Open source or paid — let's talk.

Contact Me
© 2020-2026 XPLOIT FZE. All trademarks, names and logos belong to their respective copyright holders.