Course contentsShow
← Unity Game Development
Lesson 220 of 8284. Prefabs and Data ManagementFree lesson

Implementing Prefab Variants for Specialized Assets

You will create specialized versions of existing prefabs that inherit core functionality while allowing for unique visual or behavioral adjustments.

In this lesson, you’ll learn how to leverage Prefab Variants to create unique versions of your game objects without losing the core functionality of your original design.

The Concept of Inheritance

In our previous lessons on modular design, we established that a "Base Prefab" contains the primary scripts, rigidbodies, and components that define an object's core behavior. However, imagine you have a Base_Trap prefab that uses a Hinge Joint and a Trigger script. If you want to create a "Fire Trap" version and a "Spike Trap" version, you shouldn’t create two entirely separate objects from scratch.

Instead, you use Prefab Variants. A variant is a child of the original prefab. It inherits every component and setting from the base, but allows you to override specific properties (like colors, particle systems, or even specific script variables) while keeping the underlying structure linked to the parent.

How to Create a Variant

To create a variant, simply drag your existing prefab from your Project window into the Scene, right-click it, and select Prefab > Create Variant. This generates a new asset in your project that acts as an "inheritor."

Here is how a specialized script might interact with these:

public class TrapBase : MonoBehaviour {
    public float activationForce = 10f;
    public virtual void TriggerTrap() {
        // Core physics logic that all traps share
    }
}

// In your FireTrap Variant:
public class FireTrap : TrapBase {
    public override void TriggerTrap() {
        base.TriggerTrap(); // Keep the physics logic
        PlayFireParticles(); // Add the specialized visual
    }
}

Why Use Variants?

If you decide to change the Rigidbody settings or the Hinge Joint constraints on your Base_Trap later, every single variant (Fire, Spike, or otherwise) will automatically receive that update. You maintain the "source of truth" in the base prefab while allowing your specialized assets to have their own unique visual flair and logic tweaks. This prevents the need to manually update dozens of similar traps across your project whenever you refine your physics settings.

Key Takeaway: Prefab Variants allow you to build specialized assets that inherit core physics and logic from a parent, ensuring your game remains easy to update while supporting complex, unique object behaviors.