Oftentimes while creating games you find yourself needing to reuse objects in multiple places. Maybe it is an enemy or perhaps even a tree. You create the first one and select duplicate to make a copy of the object. Now, you have to rotate and reposition it to fit its new location. When you create a new scene, you have to recreate the object from scratch. When this happens, you will most likely forget to adjust a value or add a component in the same way you did before. This creates bugs and strange behavior you did not expect. There has to be a better way, and there is. To save time and create a more efficient game, you can utilize Unity’s Prefabs to accomplish the same thing with a lot less frustration.
Unity’s Prefabs allow you to create and configure a GameObject, with all of its components and child GameObjects, once and reuse it throughout your game. They can be as simple as a sprite with a collider, or they can be as complex as a multiplayer score tracking system.
In this article, we are going to discuss what a prefab is, how to use it, why you need them, and some specific scenarios you might find yourself in.
Prefabs are assets in Unity that act as a template for GameObjects. Any changes or updates to a prefab affects every location the prefab was used. Once a prefab has been moved into the scene, values and properties can be updated individually to match the use case for that instance of prefab.
What is a Prefab in Unity
Inside of the Unity editor, you can quickly locate Prefabs by their unique blue color. All prefabs will have this color even if they are nested under other objects. The prefab blue color is observable inside the project window as well as in the hierarchy window.
What is the Purpose of Creating a Prefab
Creating prefabs serves three immediate purposes. First, using prefabs over individual objects naturally creates a clean project structure. If you are manually creating similar objects across your game then it will be filled with GameObjects that seem unrelated and clutter the hierarchy. GameObjects that perform similar functions and contain similar components should be turned into prefabs to make them more recognizable while developing.
Second, using a prefab cuts down on the amount of work you need to do creating objects. Take our previous example of a tree. While trees add to the environment and fill out your game, players do not spend a ton of time examining them. This means handcrafting every single tree is ultimately a waste of time as the effort will go unnoticed. Instead, create a handful of trees and turn them into prefabs. You can then place them around your game, rotating and scaling them to achieve the same look with much less effort.
Third, prefabs save time when tweaking your game. If you are using an enemy prefab and decide to update the model or sprite, you only have to update the prefab. Making a change to the prefab propagates to all instances of that prefab. If you were not using prefabs you would have to visit every single enemy in your game and update them manually. When done this way you are bound to miss one or two objects cause bugs and unintended behaviors.
Are Prefabs GameObjects
Unity’s prefabs are GameObjects. Prefabs can also contain multiple GameObjects. Every prefab has one top-level GameObject that forms the root of the prefab. From there you can attach components or nest GameObjects under the root/parent. There is no limit to the size of the prefab. They could contain just one GameObject or it could contain dozens.
How Do I Make a Prefab in Unity
Making a prefab is simple. You first create your GameObject inside of Unity’s scene window or hierarchy. Make sure to attach all of the desired components and default settings. If you need multiple objects in the prefab, you can nest them under an empty GameObject to create a parent for the prefab. Once you are done with the setup and configuration, drag the GameObject from the hierarchy window into the project window. This will create a blue asset in the location you dropped it. You will also notice the object you created in the hierarchy is now blue. Feel free to delete the GameObject from the hierarchy if it is not needed there. It will not affect the prefab asset in the project window.
How Do You Use Prefabs
To use a prefab, you simply drag it from the project window to the scene window or into the hierarchy. This will create an instance of the prefab. Now, you will most likely need to adjust settings or set fields or properties on the instance you just created. For example, set the rotation/scale of a tree or the patrol zone for an enemy. You can make those changes directly to the instance without affecting the rest of the objects that use the prefab. You can also create objects from prefabs inside of your scripts. It is as easy as calling instantiate and passing in the prefab. You can learn more about using prefabs in code in our article about Instantiating Prefabs in Unity
To change the values of all prefabs, double click on the prefab asset in the Project window to open it in the scene view. The properties for the prefab will be visible in the inspector and you can make changes here. Click ctrl/cmd + S or click the back arrow in the hierarchy tab to save the changes to the prefab. These changes will apply to all instances of that prefab.
Nested prefabs are prefabs contained inside of other prefabs. Using a nested prefab gives you the ability to reuse smaller components throughout your game even when they are part of a larger GameObject you will reuse. They offer the same flexibility and usability as other prefabs. Nested prefabs can also contain prefabs. There is no limit to the number of prefabs that can be nested.
An example of a nested prefab is a health bar. For multiplayer games where every player has a health bar, the health bar would be a prefab. A health bar that contains hearts, like the Legend of Zelda, would also contain multiple hearts. These hearts would also be prefabs. Every player would have a prefab health bar that contains prefab hearts. Now, if you wanted to change the hearts to skulls, you only have to open the heart prefab and change the sprite. Each player would receive the updated image without having to open each player or even open each heart in the health bar.
What is a Prefab Variant
A prefab variant is a prefab that contains a set of configurations that override the base prefab. For example, if you have an enemy that has a green sprite, fires a single rocket, and takes one hit to destroy. You need to make this enemy stronger at higher levels. This could be accomplished by making a new prefab with the needed configuration, but then if you had to change the base behavior you would have to do it in two places.
Instead, you can create a variant by right-clicking the base prefab and selecting Create > Prefab Variant. Now, you can update the sprite to a red sprite with armor, have the enemy fire two rockets, and take ten damage to destroy. All of this without affecting the base prefab. Then, if you decide to change the projectile to an arrow, you only have to update the base prefab and the changes will be replicated to the other enemies.
You can undo a prefab by right-clicking the prefab in the hierarchy window and selecting Unpack Prefab. The GameObject with lose the prefab blue color and you will be free to make changes to the object without affecting other prefabs. However, it will no longer receive any updates you make to the prefab it was created from.
If you want to remove a prefab completely, you can just delete the prefab from the project window. Any instances of that prefab in your game will still exist but will now be regular GameObjects.
And now you are ready to create and use Unity’s prefab to speed up your game development. Thank you for stopping by. Stick around and check out more of our tutorials or posts like our piece on The Unity Asset Store: How to License Content for Your Game. Also, leave a comment telling us what you liked or did not like about the tutorial. Was it easy to follow along? What do you want to learn next? As always check out some of our published apps.