Sunday, July 10, 2011

Instantiate Prefab in Your Game Tutorial

Prefabs :

A Prefab is a type of asset -- a reusable GameObject stored in Project View. Prefabs can be inserted into any number of scenes, multiple times per scene. When you add a Prefab to a scene, you create an instance of it. All Prefab instances are linked to the original Prefab and are essentially clones of it. No matter how many instances exist in your project, when you make any changes to the Prefab you will see the change applied to all instances.
For Detail You Can Refer from Here
Now Lets See How to use it in Game ..
Just made one sample project in Unity3d . Like
Steps :
1) Go Into File
2) Select New Project
3) Now Create new Scene
4) Take An Empty Object
5) Now From Assets -> Create New -> Prefab ..

Now Create New C# Script name PrefabDemo.cs
Now write in that :

using UnityEngine;
using System.Collections;

public class preafabDemo : MonoBehaviour 
public GameObject enemy; 


Now For instantiate Object You Need to write :

// Use this for initialization
void Start () 
      Instantiate(enemy,new Vector3(3,10,0),Quaternion.identity);
So For Continue Instantiate of Object
// Update is called once per frame
void Update () 
Instantiate(enemy,new Vector3(Random.value * 23.0f,Random.value*25.0f,Random.value * 24.0f),Quaternion.identity);

So ,From This You can instantiate Object in your game continue .    
You Can Download Source Code From Here

Vivek P Shah
Game Developer In Unity3d And Cocos2d
Skype : viveks2012


  1. Thanks, that helped me a lot!
    Unity's reference is good, but doesn't actually work.
    There is also the fact that you have to assign the object to be instantiated later in the editor, something that no one seems to point.

  2. AAb Ki Bar Modi Sarkarr..............