Sharp Coder is reader-supported, meaning when you buy through links on our site, we may earn an affiliate commission.
HomeBlogUnity

Unity 3D Working With Prefabs

1009

In Unity, Prefab is a template from which new objects can be created.

Cases where Prefabs are useful:

  • Environmental Assets (Trees, bushes, rocks, etc.)
  • NPCs (Non-player characters)
  • Projectiles
  • Player Instances

Intro

When dragging & dropping models directly into the Scene, they will often be spawned with some issues, for example:

  • Missing textures
  • Wrong scale
  • Wrong rotation

Tree Model with Default Material

The issues above can be easily fixed in the Scene (assigning the correct material(s), changing the scale/rotation, etc.):

Tree Model with a Correct Material

However, if you plan to use the model repeatedly and/or in various Scenes, repeating the steps above will quickly become a tedious task. That's where Prefabs come in handy.

Instead, we make changes once and then save the edited model to a Prefab, which acts as a template.

Creating a Prefab

  • To create a Prefab, simply Drag & Drop the Object from the Scene view into the Project view:

Unity 3D Create a Prefab

After the Prefab is created, it can be dropped into any Scene, creating the exact copy:

Unity Prefab Project View

Editing Existing Prefabs

Editing Prefabs can be done in 2 ways: by modifying them directly or by modifying them in the Scene.

Modifying Directly

  • Select the Prefab in the Project view, then in the Inspector View click "Open Prefab"

  • The Prefab will be opened in the editor, where you can modify it.

  • When you're done modifying, click the back arrow in the Hierarchy View, to save the changes.

Unity 3D Save Prefab Changes (Method 1)

Modifying in The Scene

  • Drop the Prefab into the Scene view then edit it there.

  • In my case, I have added another Object to the Hierarchy (Notice the (+) symbol near the icon)

With this method, changes are not saved automatically, instead, you need to manually select the root object of the Prefab, and in Inspector View click on "Overrides" then click "Apply All" to save the changes, or "Revert All" to revert the changes.

Some actions are not supported when editing Prefab directly in the scene. For example, if you need to restructure the hierarchy, you will be presented with a message "Cannot restructure Prefab instance" and with options to either "Open Prefab" or "Cancel". If you click "Open Prefab" it will open the Prefab using Method 1, which sometimes is not desirable.

To avoid opening the Prefab, click "Cancel" then right-click on the Prefab instance and click "Unpack Prefab" (alternatively you can click "Unpack Prefab Completely", in case there are child prefabs nested within the hierarchy, which you want to unpack as well).

Unity 3D Unpack Prefab

Unpacking Prefab will remove its association with the main Prefab, thus making it fully editable in the Scene.

To save Prefab back, select the folder where the original Prefab is located, then drag & drop the modified object from the Hierarchy View into that Prefab.

Unity 3D Save Prefab Changes (Method 2)

Sometimes (if the changes are too drastic), you will be presented with a confirmation message.

Make sure to double-check that you're modifying the right Prefab then click "Replace Anyway" to finalize the action or "Don't Replace" to cancel the action.

Unity 3D Replace Prefab Confirm Message

The new Prefab is now saved and ready to use!