
Objective
Leverage an Object Oriented Inheritance approach to managing common attributes of a collection of similar Unity Prefabs.
Solution
Unity Prefab Variants offer a form of Polymorphism allowing us to create a single working Prefab and then define new Prefabs as a “variation” of the original. Changes to common values will be propagated to all Variants unless the value has been previously modified (morphed) in the Variant.
Setup
It’s worth reviewing the Unity documentation found here. However, it will fall very sort of being complete and practical.
While it is possible to reuse your existing prefabs it is easier to just recreate new prefabs from scratch. This will allow you to keep your existing, working prefabs for comparison and validation. The old ones can then be removed. This may also be a good time to cleanup your file structure because it is likely that has gotten cluttered up also.
Implementation
Step 1: Decide which of your prefabs will be your “Base” prefab. This will likely be the first one you created.
Step 2: Then create a new folder in your Hierarchy and within that, right-click and create a new Prefab. Quickly copy over all the same components. TEST to make sure it works! Swap this one out for your previous prefab and test. If you find you missed something, refer back to the original.
Step 3: Once this works, right-click on the new prefab and select Create -> Prefab Variant. You may have to look hard for this because you have probably trained your eye to skip over it! Notice the differences in how the variant is displayed in the Inspector and Hierarchy.


Step 4: At this point, you may find the only change you need to make is to remove the original Script Component and add the correct one. Note: because it is a Variant, the Inspector will actually show that an inherited component has been removed.

What Happens Now
For the most part, it is that simple. Make a change in the Base Prefab, and it will cascade to the Variants. This includes changing values and adding/removing components.
Pitfalls
While the basic operation is straight forward, working with Prefabs and their Variants in the Editor does have some major pitfalls compared to working with code. Remember, your application does not run from the Unity Editor. The Editor instead writes code for you. That means you are more than a little at the mercy of what the Editor can and cannot do and “Undo” is dicey at best.
As such, it is important to take note of some pitfalls I found surprising and not easily remedied. Many of these may have proper solutions, but they are not yet known to me. Therefore, be sure to look before you leap!
- You cannot remove components in a Variant to continue inheriting it. Components are copied to the Variants rather than “inherited” in the normal code sense. Think of it more as an override/polymorphism rather than inheritance.
- Changes in the base are copied to the Variants if that component is still there and that value has not been previously overridden in the Variant.
- Changed values in a Variant will be shown with bold labels in the Inspector.


- If a change has been made in a Variant, a change to the same value in the Base will NOT propagate to the Variant.


- Changes made to the Variant will be propagated UP to the Base if you apply overrides. That is probably not what you want so don’t do that.

- When just looking at Prefab Variants in the Inspector, overrides are not obvious. You must Open the Prefab to see them.


- Selective Revert Window
- Selective Revert shows a side-by-side comparison. However, while you can change a value back, it does not “revert” the status. The Editor still considers it an “overridden” value. You can only revert all changes to that component using the “Revert” button.
- When in the Revert window, “Apply” in this context applies UP to the base, not the variant. This is the same behavior as overrides.
- This may change your work flow and lead you to experiment, then revert, then re-apply only the final changes you want.
- Be careful of revert all. This reverts everything, including removing added components and restoring deleted components. Unless you have thoroughly dorked things up, this is probably not what you want to do.
- To apply overrides consider the context.
- Making changes to a Variant and applying the override applies it UP to the Base.
- Dragging the Base Prefab to the Hierarchy making changes and applying the override propagates to the Base prefab, which in turn propagates to the Variants.
- Dragging a Variant Prefab into the Hierarchy, making changes and applying the overrides propagates the changes to just that Variant Prefab. That Variant has now overridden that value and will no longer be affected by changes to the Base for that value.
- Do not rename the base Prefab or its Variants!!! This causes the Editor to pretty much lose its mind, and all your changes. So, when naming, choose wisely…
Wrap Up
Prefab Variants provide a way to manage Game Objects in an hierarchical manner. However, they do not work the same as code and so can be a bit finicky. With a little caution, Prefab Variants can provide a powerful mechanism to manage and maintain behavior of related Game Objects.
