Prefabs
Manual     Reference     Scripting   
Unity Manual > User Guide > Building Scenes > Prefabs

Prefabs

A Prefab is a type of asset -- a reusable GameObject stored in Project View. Prefabs can be inserted into any number of scenes, multiple times per scene. When you add a Prefab to a scene, you create an instance of it. All Prefab instances are linked to the original Prefab and are essentially clones of it. No matter how many instances exist in your project, when you make any changes to the Prefab you will see the change applied to all instances.

Creating Prefabs

In order to create a Prefab, you must make a new blank Prefab using the menu. This blank Prefab contains no GameObjects, and you cannot create an instance of it. Think of a new Prefab as an empty container, waiting to be filled with GameObject data.

A new, empty Prefab. It cannot be instanced until you fill it with a GameObject.

To fill the Prefab, you use a GameObject that you've created in the scene.

  1. Choose Assets->Create->Prefab from the menu bar and name your new Prefab.
  2. In Hierarchy View, select the GameObject you wish to make into a Prefab.
  3. Drag & drop the GameObject from the Hierarchy onto the new Prefab in Project View.

After you have performed these steps, the GameObject and all its children have been copied into the Prefab data. The Prefab can now be re-used in multiple instances. The original GameObject in the Hierarchy has now become an instance of the Prefab.

Prefab Instances

To create a Prefab instance in the current scene, drag the Prefab from the Project View into the Scene or Hierarchy View. This instance is linked to the Prefab, as displayed by the blue text used for their name in the Hierarchy View.

Three of these GameObjects are linked to Prefabs. One of them is not.

Inheritance

Inheritance means that whenever the source Prefab changes, those changes are applied to all linked GameObjects. For example, if you add a new script to a Prefab, all of the linked GameObjects will instantly contain the script as well. However, it is possible to change the properties of a single instance while keeping the link intact. Simply change any property of a prefab instance, and watch as the variable name becomes bold. The variable is now overridden. All overridden properties will not be affected by changes in the source Prefab.

This allows you to modify Prefab instances to make them unique from their source Prefabs without breaking the Prefab link.


A linked GameObject with no overrides enabled.

A linked GameObject with several (bold) overrides enabled.

Breaking and Restoring Prefab Links

There are certain actions which will break the link between a single instance and its source prefab, but the link can always be restored. Actions that will break the link:

These actions will prompt a warning/confirmation message about breaking the link. Confirming the action will break the link so changes to the source Prefab will no longer affect the broken instance. To restore the link, you can click either the Reconnect or Apply buttons in the Inspector of the instance.

Imported Prefabs

When you place a mesh asset into your Assets folder, Unity automatically imports the file and generates something that looks similar to a Prefab out of the mesh. This is not actually a Prefab, it is simply the asset file itself. Instancing and working with assets introduces some limitations that are not present when working with normal Prefabs.


Notice the asset icon is a bit different from the Prefab icons

The asset is instantiated in the scene as a GameObject, linked to the source asset instead of a normal Prefab. Components can be added and removed from this GameObject as normal. However, you cannot apply any changes to the asset itself since this would add data to the asset file itself! If you're creating something you want to re-use, you should make the asset instance into a Prefab following the steps listed above under "Creating Prefabs".

Page last updated: 2010-09-09