In this blog about Prefabs I will show you how to create a prefab and how edit it. Prefabs is one of the most important part of the game development in Unity.
1. What is a prefab?
Imagine that you want to create an object, let say an antorch, and you copy-paste this gameobject all over your scene but when you have done all of this you will have 100 antorches, and imagine you need to make a change in the antorch, in this previous case you will need go one by one making the change, with the objective of avoid this unnecessary task you have to use a prefab.
The prefab is like template of a game object. It save the components and properties of a game object and let you create instance of this gameobject, as all the instance come from one prefab if you make a change in the prefab asset it can be applied to all the instances at once.
There are properties that are no saved in the prefabs like position, rotacion, width, height, margins, anchors and pivot, the reason to not save this properties is to prevent that all the instance of the prefab appear in the same position in the scene overlapping each other.
2. Creating a prefab
Before you create a prefab, I recommend you to create a folder with the name of “Prefabs” to give to your project some order.
After that any game object that you want to convert in a prefab just drag it into the the window project and that is all. In the herarchy window the color of the gameobject will changed to blue and there is asset in the folder prefabs.
3. The buttons in the prefabs
Every time you select an instance of the prefab you will find three buttons that are no present in a normal gameobject, there buttons are: open, select and overrides.
- Open: this allow you to open de prefab mode (that is a isolation mode to edit your prefabs)
- Select: This will find the prefab asset in your project window
- Overrides: let you to override your prefab from the instance to the asset
4. Editing a prefab
Well sometimes you want o make cnahges i your prefabs, making the same change to all the instance of the object, to achieve this there are many ways, but in this part i will show you the mos easy one.
Go to your project window and doble click on your prefab asset, this will change the color of the scene (usually to blue) and allow you to make changes on your prefab that will affect all its instances, one exception to this are the instances that you have modified previously in the herarchy window.
5. Autosave in prefabs
The autosave is one easy way to save time, remember always set on.
6. Modifying the prefab from a instance of it
May be one day you want to change a prefab but you don’t want to search for the prefab asset, you can make changes using a instance of that prefab. After you make your change go to the inspector and make click on Override. At this opint you can override the component with the change or you can override all the changes with only one clik on Apply all.
7. Creating variations of a prefab
May be you don’t want to overrides all the changes to the prefab asset, well this is possible and with this you create a variation of your prefab, but there is limitations to this, for example , you cannot reparent a gameobject or remove a gameobject that is part of the prefab, a solution is to deactivate the gameobject that you want to remove.
And don’t forget to check out the rest of the blogs and our games!!