THIS CONTENT DOWNLOAD SHORTLY

Objective

This post introduces you to Prefab in Unity3D.

 

 

Step 1 Prefab Introduction

What is Prefab?

Actually prefab is the copy of the game object. Using prefab we can store game objects with its properties and components already set. So we can reuse it in many ways. It contains a hierarchy of game objects. In other words we can say that prefab is a container which could be empty or contains any number of game objects.

 

Step 2 Requirement of Prefab

Why do we need Prefab?

In unity you can create anything using prefab - as complex as you like, and save it out for later use. One of the advantages is that, it allows you to edit in original and update on other copies of it directly. So we don't have to go and change in individual object.

What is the difference between Game object and Prefab?

When we duplicate the game object, they all work independently. So if we want to modify any object, we have to make changes in all the copies of the object.

Prefab solves this problem. In prefab the above process will be done automatically. We just have to modify prefab and all the instances will be automatically updated because they maintain connection with the original prefab and will reflect all the changes that are made.

 

Step 3 Create Prefab

How to create Prefab?

You can create prefab using following techniques:

  • In asset, right click and create Prefab, then drag the object from scene to the empty prefab object.
  • Drag any game object onto project view to make a prefab. When we drag prefab from project view to scene view it will create the instance of it and will have blue text in hierarchy.
 

Step 4 Use of Prefab

Where can we use Prefab?

To illustrate the strengths of prefab, let's consider basic situation where they would come in handy:

  • Bullet shooting
  • wall building
  • Rocket launcher

When we have to instantiate object at run time, prefab can be used. Let's take one scenario in which whenever we press a button, a sphere or ball should appear in the scene.

 

Step 5 Demo

 

5.1 Create Project

Create project and take one plane and one sphere game object in the scene. 

create-project

 

5.2 Create Prefab

Now let's create prefab, just click on assets and right click > > Create prefab

Now drag and drop sphere object from hierarchy view to project view as show in the image.

This will create a prefab of sphere.

Delete the sphere in hierarchy view.

drag-and-drop-sphere-object

 

5.3 Create Instance

Instance will be created when we drag and drop that prefab from project view to scene view. Created instance is in color blue. 

instance-is-blue-in-color

 

5.4 sphereCreator script

When the user clicks on plane, instance of ball prefab will be create.

Make a sphereCreator.cs script and write the code given below:

public class SphereCreator : MonoBehaviour {
//Refrence of Prefab.
publicGameObject ball;
 
intxPosition=0, yPosition=3;
 
    //This will call whenever user click on plane.
voidOnMouseDown()
    {
Instantiate(ball, new Vector3(xPosition,yPosition,0),Quaternion.identity);
xPosition += 1;
    }
}

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

 

5.5 Apply Script

Now apply this script to plane game object as show in the image. Give reference of prefab as show in image. Now hit the play button and check.

play-button-and-check

hit-the-play

 

5.6 Change Color

Now what we have to do is, whenever a user clicks on the ball its color should change to green. We are going to use a script to achieve this thing. Make another script named PrefabScript.cs

And write the code as given below:

voidOnMouseDown()
{
    this.renderer.material.color = Color.green;
}

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

color-will-change

color-changed

 

5.7 Apply Rigid Body

Now if you want to give a rigid body to the object, we just apply it to the original prefab and it will update it on all the instances. This is the advantage in prefab.

If it wasn’t a prefab then we would have to give this component to each and every object. 

component-to-each-ojbect

prefab-works

This is how prefab works.

I hope this blog is very helpful to you while working with Prefab in Untiy3D. Let me know if you have any questions regarding Unity3D please comment here. I will reply you ASAP.

Got an Idea of Game Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best Game Development Company in India.

I am professional game developer, developing games in unity (for all platforms). I am very passionate about game development and aim to create addictive, interactive, high quality games.

face mask Belial The Demon Headgear Pocket Staff Magic