268

Share

GAME DEVELOPMENT

How To Use Prefab In Unity?


Prefab is a pre-configured, reusable GameObject that is created with components in the scene and stored in the project. It is like a blueprint of a GameObject.

Agnesh Pipaliya

July 21, 2022

Prefabs are the most requested feature of Unity. It is one of the first things that beginners have to learn while using Unity. 

The prefab acts as a template from which you can create new game object examples in the scene, and any edits made to the prefab asset will be immediately reflected in all examples produced from it. You can override components and settings for each example individually as well. 

This blog will make you learn more about the prefabs in Unity.

Need Of Prefab

We can create anything with prefab - as complex as we like, and save it for later use. It allows you to any changes or edit in original and update on other copies of it directly without change in individual object.

Create Prefab

We can create prefab using following two ways:

  • Drag the game object that you want to make prefab onto project view.
  • In asset, right click and create Prefab, then drag the object from scene to the empty prefab object.

Where can we use Prefab?

  • Now to understand the use of prefab, let's consider the basic event where they would come in handy-
  • Shooting Bullets
  • Make Road, Wall Building etc...
  • If we have need to instantiate gameobject at run time, at that time prefab can be used. Let's take one demo in which whenever we press a button, a cube should instantiate and appear in the scene.

Make Scene

Create a new project and create a new Plane and one Cube in the scene.




Create Prefab

Now drag and drop Cube game object from Hierarchy to Project as shown in below the image. This will create a prefab of Cube. Now Delete the Cube in the hierarchy view.




Create Instance

The instance will be created when we drag and drop that prefab from project view to scene view or direct from project view to hierarchy. Now you can see that the created instance is with a blue color name.




Create Cube Controller Script

When the user clicks on the Plane, the instance of Cube prefab will be created.

Make CubeController.cs Script and write code as shown below:




In the above image code, the Instantiate method will create an instance at run time. In an argument we have to specify the object (that we want to create), position and rotation.

Apply Script

When the user clicks on the Plane, the instance of cube prefab will be created.

Now apply this script to the Plane and assign the reference of the Cube from Project View as shown below image.




Now run the scene and click on Plane, you can see that when user clicks on the Plane a Cube gameobject will instantiate at that time at the position and rotation that we were given in the script.




Change Color

Now what we have to do is, whenever a user clicks on the Cube its color should change to blue. We are going to use a script to achieve this thing. Make another script name with PrefabColor.cs

And write code as shown below:




Now apply this script to our Cube prefab. So when the user clicks on the particular Cube object, its color will change from white to blue.




Now run the scene click on Plane to instantiate Cube prefab as gameobject and after that click on particular Cube to change its color. When you click on Cube you can see that the color of that Cube will change from white to blue as shown below image.




Unpacking Prefab Instances

If you want to return the contents of a Prefab instance into a regular GameObject, you need to unpack the Prefab instance. This is exactly the reverse operation of creating (packing) a Prefab, except that it doesn’t destroy the Prefab Asset but only affects the Prefab instance.

You can unpack a Prefab instance by right-clicking on it in the Hierarchy and selecting Unpack Prefab as shown below.




The resulting GameObject in the Scene no longer has any link to its former Prefab Asset. The Prefab Asset itself is not affected by this operation and there may still be other instances of it in your Project.

Conclusion

Prefabs are an essential feature that you should know before developing a game in Unity. It is like a copy of a GameObject that you can duplicate and use in a scene, or use to dynamically generate GameObjects. 

Read this blog carefully and start creating a prefab on the Unity platform.

img

Agnesh Pipaliya

COO of Vasundhara Infotech, a leading Software development company in the USA. His technological interests has helped the company in making useful decisions.

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

Get a Newsletter

Sign Up to our newsletter to get latest updates staight in your inbox.

Vasundhara respects your privancy. No Spam!

message

Have a project in mind? Drop a message to Bansi Pipaliya & start the discussion!

Latest 13 Web Development Trends To Expect In 2022
April 11, 2022 Category : company news

Revealing Vasundhara’s New Identity

Read More
Leave a Comment