Prefabs in Unity!

Matej Marek
3 min readMar 31, 2021

--

Unity’s Prefab system allows you to create Prefabs - but what are they, how and when should you use them?

Prefab is basically a stored GameObject with all of its components, property values, and child GameObjects as a reusable ‘Asset.’ They act as a template for easy reuse in your Scenes.

The best time to create a prefab is to reuse a GameObject configured in some way multiple times in your project. This is because the Prefab system allows you to keep all the Prefab copies the same.

Any changes that you make to a Prefab asset can be transferred to any Prefab instances currently in use, which allows you to do changes much easier and quicker! Imagine, for example, that you wish to change a texture on one of your platform types - instead of having to go through twenty different GameObjects in three scenes, you can adjust your prefab once - and they will all get changed simultaneously!

Creating Prefabs

To create a Prefab, drag a GameObject from the Hierarchy window into your Project window - you should always create a separate folder for your ‘Prefabs.’ Prefab assets are shown with a blue cube Prefab icon, both in your Project and Hierarchy view. When you select a prefab, their file type will be a *.prefab.

When a Prefab is created and selected in your Hierarchy, new options will appear in the Inspector.

When you create a GameObject in Unity, you can edit them simultaneously - the same applies to Prefabs. However, you can edit a Prefab, and these changes can be applied to all instances of that Prefab, both in use and in your ‘Prefabs’ folder. But you can also edit them as a single instance, where you change its properties for a singular purpose. You can override settings on an individual prefab and create different Prefabs variants.

  • Open - This button allows you to open your Prefab in Prefab Mode.
  • Select - This button opens up your ‘Prefabs’ folder and highlights the original Prefab in your Project window.
  • Overrides - This dropdown will show you all the changes that have been made to this instance of prefab. You can view each change separately when you click on it - here, you can Revert or Apply each change individually. Alternatively, you can click on the Revert All or Apply All buttons at the bottom.
  • Revert All - This button will revert all the changes in the overrides list.
  • Apply All - This button applies all changes made to this instance of Prefab, applying them to all other existing instances and the original Prefab in your ‘Prefabs’ folder.

Using Prefabs

You can easily add prefab instances to your scene manually, or you can instantiate them at runtime. This will be covered in the next article!

Examples of when to use prefabs:

  • Projectiles - For example, a laser that your player/enemies shoot.
  • Enemies - Enemies that try to do bad things to your player!
  • Player - The player can be re-spawned when killed or when another scene/level is loaded and needs to be spawned.
  • NPCs (Non-player characters ) - For example, a merchant that might appear every few levels.
  • Environmental assets - For example, platforms, trees, clouds, etc.…

You can also nest Prefabs inside other Prefabs, which can create ‘prefab object hierarchies’ - read more about that here!

Overall, Prefabs are an important part of Unity, and knowing how to use them is a must!

But that is it for now, thank you for reading and feel free to follow me for more articles — and as always, good luck and see you next time!

--

--