Developing Game in Unity? Check Template Projects, Tools, VFX & many more!

Unity 3D Working With Prefabs

232

Prefabs provide a way to store Game Objects along with their components, scripts and other data to be used later. Basically Prefab is a template, from which new objects can be created in the Scene.

Cases where Prefabs are useful:

  • Environmental Assets (Placing trees, bushes, rocks etc.)
  • NPCs (non-player characters) (Spawning enemies)
  • Projectiles. (Spawning bullets)
  • Main Player Instance. (Spawning our player)

Creating a Prefab

I will be using models from this free Low Poly Tree Pack.

Usually, when we drag & drop a model directly into the Scene, it has some issues, like:

  • Missing textures/materials
  • Wrong scale
  • Wrong rotation etc.

Tree Model with Default Material

We can easily fix it in the Scene by 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.

  • To create a Prefab, simply Drag & Drop the Object into the Project view.

Unity 3D Create a Prefab

After the Prefab is created, you can drag & drop it into a Scene to create an exact same copy.

Unity Prefab Project View

Editing Existing Prefabs

Editing Prefabs can be done in 2 ways, by opening the prefab (Method 1) or by modifying prefab directly in the Scene then applying the changes (Method 2).

Method 1

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

  • The Prefab will be opened in a Scene view where you can modify it.

  • After you done, click the back arrow in the Hierarchy View. The changes will be saved and applied to every spawned prefab instance across the Scenes:

Unity 3D Save Prefab Changes (Method 1)

Method 2

  • Drop the Prefab into the Scene then start making the changes you need (Changing scale, adding/removing objects etc.)

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

NOTE: 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. Clicking "Apply All" will modify the main Prefab along with all of its spanwed instances.

NOTE 2: 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 hierarchy which you want to unpack aswell).

Unity 3D Unpack Prefab

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

To save the Prefab back, select the folder where Prefab is located, then drag & drop the modified Object from the Hierarchy View into the 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

Congratulations, the new Prefab is now saved and ready to use!