When making games, you often have identical enemies that attack the player. In Minecraft, there are skeletons, creepers and zombies. In Doom, there are Imps, Cacodemons and Hell Knights. Even a game like Stardew Valley has bats, slimes and bugs.
But games frequently require other repeating assets such as clouds, torches and well, anything and everything.
With Unity, you can create templates of GameObjects, and store them in your Project window to be created on demand. These are known as Prefabs, and you’ll be using them all over your game.
Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.
Setting up the alien
Creating a Prefab is straightforward. All you need is a working GameObject. You actually have an Alien model in your model in your Models folder. Select the Bobble-Enemy-Body. At the bottom of the Inspector, you’ll see a problem.

This alien is seriously broken. This may be the result of a misconfiguration of the model file, but it isn’t salvageable. Thankfully, co-author Eric van de Kerckhove rebuilt this model from the ground up. Eric is an accomplished writer, game developer and artist. Check him out over here.
In the Project window, select the BobbleEnemy-Body and delete it. You’re going to keep the BobbleEnemy-Head.
Next, download this file:
Unzip it and drag it into your project into the Models folder. If you select the alien in Unity, you’ll see your little monster.

The alien appears to be missing its skin. If you select the Materials tab, you’ll see a notice letting you know that there are materials included with this model. Remember, the materials add color and lighting information to a model. You’ll learn about materials in the next tutorial.

Just about the message, you’ll notice an Extract Materials button. Click it and select the Materials folder in your project. Now to add the color.
Open the Materials folder and select the Main Texture.001 material. In Surface Inputs category, find the Base Map property. It should be the very first property. Click the tiny circle next to it.

This opens a texture picker. Select the Bobble Wars Enemy texture. You can also do use the search bar or decrease the size icon size to make it a list.

Do the same for Main Texture in the Materials folder. When you select the BobbleEnemy, you’ll see it’s ready to go.

Creating a Prefab
A Prefab is a great way to reuse assets over and over again. Prefabs are effortless to create. You just need a GameObject.
In the Hierarchy, click the Create button and select Create Empty from the drop-down menu. Single-click the GameObject to name it Alien.
Select your AlienEnemy model in the Project window, and drag it into the Alien GameObject in the Hierarchy. Select Alien in the Hierarchy and, in the Inspector, set the position to (2.9, 13.6, 8.41). The space marine and the alien are now hanging out.

This game features LOTS of aliens, so this is an excellent use case for a Prefab. To create a Prefab, drag the Alien GameObject to the Prefabs category.

A few things have changed. There’s a new entry in your Prefabs folder with an icon beside it. You’ll also note the name of the GameObject in the Hierarchy is now blue with a new icon. Finally, you’ll see that there are already Prefabs in the Prefab folder. These are the Prefabs that you imported with the rest of the assets.

Note: You don’t have to drag your model into that specific folder to create a Prefab — all that’s required is dragging a GameObject into any folder in the Project window. Having a Prefabs folder is simply good housekeeping.
Changing a Prefab in isolation
Prefabs are great in that you can publish changes to existing models. To see this in action, drag multiple Aliens in the Scene view near the space marine. The position doesn’t matter as you’ll be deleting these soon.

There are a few ways for you to update a Prefab. One way is to update a singular Prefab and publish all the changes to the rest of the Prefabs.
In the Project window, select the Alien in the Prefabs folder. In the Inspector, click the Open button at the top of the Inspector.

You’ll notice the Scene view will turn blue and everything but the Alien will disappear. You are now in editing your Prefab in “isolation”. The entire scene is hidden so you can focus just on your Prefab.

In the Hierarchy, select the BobbleEnemy and disable it by unchecking the checkbox next to the name.

Now you need to return to the normal Scene view. Just beneath the Scene tab, click the Scenes link.

Now, when you return to the normal Scene view, all the aliens are gone. Your change was published to every Alien.
Reverting or publishing Prefab changes
Often times, you’ll want to override properties or behavior for a Prefab. Unity lets you do this. When you modify a Prefab instance, you’ll have the option to revert the changes or even publish those changes to the other Prefabs.
Select an Alien in the Hierarchy. Expand it and select the BobbleEnemy child in the Hierarchy. Enable it so that you can now see a visible Alien. Notice the other aliens are invisible. You’ve just overridden a Prefab property.
Select the top-level Alien GameObject, and click the Overrides button. This expands a new dialog that provides detail about your overrides.

The Revert All removes any of your changes. The Apply All applies your changes to the rest of the Prefabs. Click the Apply All button and all your aliens will reappear.
Changing your Prefab in context
There are times when you want to edit your Prefab in the context of the scene. This is quite useful to see how your changes will appear alongside everything else.
Right-click your Alien in the Hierarchy. From the context menu, select the Prefab category. You’ll see you have lots of options. Select the Open Asset in Context option. The scene will now turn white.

In the upper right-hand portion of the Scene view, you’ll also some additional options for you to customize your editing. For instance, if you need to see the complete scene, select the Normal tab.

Like editing in isolation, you return to the normal Scene view by clicking the Scenes link at the top left of the Scene view.
Return to the Scenes. You don’t need droves of aliens yet, so delete all the Aliens from the Hierarchy.
Where to go from here
Prefabs are a key component in Unity. You’ll be using them throughout your games. This raises an essential question: when do you use a Prefab? Personally, I start with a plain old GameObject. Should I need to make another instance of it, I convert it to a Prefab.
You’ll be using lots of Prefabs in this tutorial series. To learn more about Prefabs such as nested Prefabs and Prefab Variants at the official documentation.
That said, you’ll take a short break from Prefabs for the moment. It’s time for you to take a closer look at Materials. You’ll do this in the next tutorial.
Discover more from Jezner Books
Subscribe to get the latest posts sent to your email.