Manage GameObjects with prefabs
Tutorial
·
foundational
·
+10XP
·
30 mins
·
(8103)
Unity Technologies

In this tutorial, you’ll use prefabs to design, create, and organize many GameObjects in a scene. By the end of this tutorial, you will be able to:
- Explain how to use prefabs in a scene.
- Add a prefab to a project.
- Edit a prefab in prefab mode.
- Create a prefab variant.
- Apply or revert changes to a prefab variant.
- Nest prefabs.
- Describe the process and outcomes for changing a nested prefab or prefab variant.
Languages available:
1. Overview
In the Unity projects you create, you’ll typically have many GameObjects to manage. In many cases your GameObjects will be copies of others — for example, in a first-person shooter game there could be many copies of the same enemy character, the same boulders to hide behind, and the same trees, weapons, and objects to collect.
As you design these GameObjects, you might want to make changes to all copies of one item. You might even want to change all the trees into cacti! If there are many trees, such a change would take a lot of work. However, instead of managing many copies of items, you can organize your duplicated GameObjects using prefabs.
A prefab is an asset that acts as a template of a GameObject. From the prefab, you can create multiple copies, called instances. A change to the prefab asset causes all of its instances to change as well. To change many trees into cacti, you could just change a single prefab tree!
In this tutorial, you’ll become familiar with prefabs. You’ll use prefabs to create and manage many falling objects in your current project.
2. Create a prefab
To make your scene more fun, use prefabs to make many more falling bouncy balls. First, make a prefab to serve as your prototype bouncy ball:
1. Rename your original falling sphere GameObject to “BouncyBall”.
2. Make sure BouncyBall has a regular material and a Physic material. Adjust the size, too, if you like. You are about to create many copies of this sphere.
3. As you did with materials, make a folder for prefabs. In the Project window, select the Assets folder and right-click to select Create > Folder.
4. Rename the new folder to “Prefabs”.
5. Open the folder so that the right pane of the Project window (in a Two Column Layout) shows the empty folder, where you’ll see the message “This folder is empty”.
6. From the Hierarchy window, drag BouncyBall into the Prefabs folder. The new asset in your Prefabs folder with the blue cube icon is your prefab.
Note: Any folder in your project can contain prefabs, not just the Prefabs folder. However, using a Prefabs folder is good practice.
In the Hierarchy, the BouncyBall GameObject is now also blue to let you know that this object is an instance of your prefab.

3. Create prefab instances
With your prefab created, it’s easy to make a lot of bouncy balls to fall onto your structure:
1. From the Prefabs folder in the Project window, drag the BouncyBall prefab into your scene.
2. Drag several more instances into your scene, up to 10.
3. Position the new BouncyBall instances so that they will fall on your structure. For more bounce, position them higher (increase Position Y)!
Tip: Hold down Shift in either the Scene view or the Hierarchy to select all your new instances at once, then position them as a group.
4. Run your application and watch the spheres fall and bounce!
So far, this is not too exciting, right? After all, you could have duplicated BouncyBall several times to get the same results without using a prefab. But keep going — in the next step, you’ll see the power of prefabs.
4. Update prefab instances in prefab mode
What if, for example, you wanted all your BouncyBall instances to be a different color? You can update the color all at once by updating your prefab in Prefab Mode, a special editing state in which you can change a prefab in the Scene window.
To enter Prefab Mode and edit your prefab:
1. In the Hierarchy, select a prefab instance and then select the arrow on the right side of the row.

Tip: By default, Prefab Mode appears in the context of your scene. To edit your prefab in isolation, press Alt while selecting the arrow.
In Prefab Mode, the prefab is the only GameObject you can edit.

2. In the Project window, open your Materials folder and drag a different material onto the prefab in the Scene window. Select a material that will be noticeably different.
3. Select Save near the top right of the Scene window to save your changes. (If you skip this step, you’ll be prompted to save when you exit Prefab Mode.) You can also enable Auto Save to save your edits as soon as you make them.
4. To exit Prefab Mode, select the arrow at the top left of the Hierarchy window.

All your BouncyBall instances will have the new material!
This is a simple example, but prefabs are quite powerful. Consider these examples:
- In a model of a city, there are 100 parking meters. You can change the text on every parking meter at once.
- In a prototype of a game, a colleague is modeling the enemy character while you work on gameplay. Your placeholder for the enemy is a prefab of a capsule primitive. When the enemy is ready to import, you can simply replace the capsule object in the prefab with the new model.
5. Override prefab properties
When you make a change to a property of one prefab instance, that property is called an override, since you are overriding the default prefab settings. The Inspector window helps you keep track of your overrides.
To make variations in your prefab instances with overrides:
1. In the Scene view, select one of your BouncyBall prefab instances.
While this object is selected, take a look at the top of the Inspector window. There is an additional section titled Prefab containing controls that you can use to work with the prefab. (The Open button is another way to edit the prefab in Prefab Mode.)

2. Using the Transform component in the Inspector, change the Scale on the x, y and z-axes of this BouncyBall. Make the change noticeable in the Scene window.
The blue line in the left margin of the Inspector indicates that the Scale property varies from the base prefab.

3. Using the Sphere Collider component in the Inspector, select the circle icon to change the Physic material to None. Now it will not bounce when it hits the ground.

4. Apply a different material to this GameObject so that it looks visibly different from the others.

5. Run the application. Notice that the only sphere that doesn’t bounce and has a different material is the one you edited. If you want to apply those changes to all instances of this prefab, you’ll need to apply them to the base prefab.
You have now overridden three properties one one prefab instance: the Scale, Physic material, and regular material. In the following steps you’ll do something different with each one.
6. Using the Prefab controls at the top of the Inspector, select Overrides to see a list of components that differ from the prefab.

7. Let’s say that you have changed your mind about removing the Bouncy Physic material. Select the Sphere Collider component from the Overrides list to view two versions of the component: Prefab Source and Override.

8. To return the Sphere Collider back to the original settings, select Revert at the top of the Override version of the component. This ball will change back to use the Bouncy Physic material.
9. Let’s say that you want all your bouncy balls to be as large as this one. To apply the override in the Transform component to the base prefab, return to the Overrides dropdown and select Transform > Apply > Apply to Prefab “BouncyBall”. All of the balls will change size!

Overrides are useful if you want to make variations to your prefabs one at a time or test a change before you apply it to multiple GameObjects. In the next step, you’ll see how to organize different variations of your prefabs.
6. Create prefab variants
You can create networks of variations using prefab variants, which are prefabs based on other prefabs.
Prefab variants make it much easier to create a series of interrelated prefabs that will give your project a uniform look. For example, in a project with many trees, you could create a base prefab of a tree and then create apple tree and peach tree variants of that prefab.
Animated diagram demonstrating how changes to a base prefab will change all instances of prefabs and prefab variants created from it, while changes to a prefab variant change only the instances derived from that prefab variant.
When you make changes to the base tree prefab, they will appear on all the apple and peach trees. When you make changes to the peach tree variant, they will appear only on the peach trees.
To create a prefab variant of your BouncyBall:
1. Select the BouncyBall instance you’ve been working on, which has a different material than the others.
2. From the Hierarchy, drag this instance into your Prefabs folder. You’ll see a prompt to create a new original prefab or a prefab variant.

3. Select prefab variant. (If you select Original Prefab, then changes to the original prefab won’t propagate to the prefab variant.)
4. In the Prefabs folder in the Project window, rename your new prefab something descriptive, like “RedBouncyBall.”
5. To create more of these new bouncy balls, drag new instances of this new prefab into your scene and reposition them to fall on your structure.

Now you will see how prefab variants work as you perform three experiments.
Experiment 1: Change a prefab variant
6. Open the new prefab (RedBouncyBall in our example) and change its Scale to something very large, like 5, 5, 5, and return to the Scene view.
Tip: You can double-click a prefab in the Project window to open it in Prefab Mode.
As you would expect, all the new instances get bigger, but instances based on the original BouncyBall prefab stay the same.

Experiment 2: Change the base prefab
7. Open the BouncyBall prefab in Prefab Mode.
8. In the Inspector window, locate the Sphere (Mesh Filter) component and its Mesh property. Using the picker (circle icon), change the mesh to a capsule.

All of the balls change shape, both the big ones and the regular ones, because the RedBouncyBall prefab is a prefab variant of BouncyBall, and therefore all the RedBouncyBall instances inherit these changes.

Experiment 3: Change an overridden property
9. Open the BouncyBall prefab in Prefab Mode and change its scale to 1, 1, 1 and return to the Scene view.

The original BouncyBall instances are smaller, but the RedBouncyBall instances are unchanged. This occurs because the RedBouncyBall prefab variant overrides BouncyBall on the Scale property. Changes to the scale of the original prefab are still overridden in the prefab variant.
7. Nest prefabs
You can nest prefabs within other prefabs when you need one type of object to be part of another. For example, in a game where the player collects fruit from trees, there are apple, pear, and peach prefabs. These fruits grow on apple, pear, and peach trees that are also prefabs. The fruit prefabs are nested inside the tree prefabs.
On your own, try to nest prefabs to create unusual shapes:
1. Create an empty 3D GameObject and drag it into your Prefabs folder. Give your prefab a name.
2. Edit the prefab in Prefab Mode.
3. Drag other prefabs from your Prefabs folder into your new prefab.
4. Add these objects to your scene and watch them fall!
Tip: If one or more of the objects in a nested prefab has a Physic material, then the individual prefabs will behave independently, and the complex shape will come apart when it falls. However, if none of the objects has a Physic material, the complex shapes will hold together as they fall.
8. More things to try
Now you know enough about prefabs to create an interesting and well organized collection of primitives to fall, bounce, and tumble onto your structure. Can you build a scene with 50 falling objects, or maybe more? Use prefabs to create a variety of different shapes with different sizes, materials, and Physic materials. Challenge yourself to try some of these techniques:
- Make sets of falling objects with different colors (materials), mass (Mass in the Rigidbody component), and bounciness (Physic material), all based on prefabs.
- Make complex shapes with nested prefabs and then change a prefab that you nested.
- Make a prefab variant of a prefab variant and then change properties of the original prefab. Can you predict what will happen?
9. Next steps
With prefabs, you can create and manage many GameObjects. With transforms, materials, Physic materials, and the Rigidbody component, you’re on your way to create sophisticated 3D worlds. However, you’re going to need more than cubes and spheres. In the next tutorial, you will see how to get more sophisticated GameObjects into your projects without having to build them from scratch.