How to Instantiate a Prefab in Unity

When we make games, we tend to spawn many characters and objects during runtime. In Unity instantiate is the function used for this. This enables us to spawn any game object at any point in the game world. Unity requires the game object to be prefab or available in scene hierarchy in order to spawn it. In this post, we will see how you can spawn objects in unity using instantiate function and where not to use instantiate.

What is Unity prefab?

Before we jump into Unity instantiate, you must know what a prefab is?

A prefab is a game object which has already been customized to be deployed in a game scene. For example, if you have a character in your scene. You will have to set the position, rotation, scale, and add other components required before using it. Any game object in which you add these required components and keep it ready in the resource folder for deployment is called a prefab in unity.

Instantiating A prefab in Unity

Step1: Create a Prefab

  1. Create a gameobject in the Hierarchy window.
  2. Drag and drop the gameobject to the project window.
  3. That’s it, you have your prefab. Now you can delete the gameobject from the hierarchy window.

Step2: Adding script to scene

  1. Create an empty gameobject.
  2. Add a script component and call it “Instantiate_example” (you can give it any name of your choice).
  3. Copy and paste the code below.
using UnityEngine;
using System.Collections;

public class Instantiate_example : MonoBehaviour 
{ 
  public Transform prefab;
  void Start() 
  { 
     Instantiate(prefab, new Vector3(2.0F, 0, 0), Quaternion.identity);
  } 
}

Step3: Assign Prefab to script

  1. Select the script object in hierarchy view.
  2. Drag and drop the prefab to the prefab variable in the inspector window.
Assign prefab to script for instantiating

Instantiate Prefab as a child of another object

You can do this by instantiating your prefab as a Gameobject and then assigning the parent. This method can be used to instantiate an UI prefab also. You just need to make sure that the parent is inside the canvas.



using UnityEngine;
using System.Collections;

public class Instantiate_example : MonoBehaviour 
{ 
  public Transform prefab;
  void Start() 
  { 
     Gameobject childprefab=Instantiate(prefab, new Vector3(2.0F, 0, 0), Quaternion.identity) as Gameobject;
     childprefab.transform.parent = GameObject.Find("parent object").transform;
  } 
}

Instantiate prefab in Unity by name

You can reference the prefab game object and load it from the resource folder directly using

Resources.Load()

The prefab you are referencing should be inside the resource folder

using UnityEngine;
using System.Collections;

public class Instantiate_example : MonoBehaviour 
{ 
 
  void Start() 
  { 
     Gameobject newplayer=Instantiate(Resources.Load("Player", typeof(GameObject))) as Gameobject;

  } 
}

When and when not to use instantiate

Instantiate is really useful but when using it to spawn multiple objects can fill the memory and affect game performance. Let’s see what are the ideal conditions when you can use instantiate.

  1. Spawn single characters that stay throughout the game like the player Gameobject.
  2. Objects that are limited in number like ammo, power-ups, etc. which are destroyed later.
  3. For effects on objects like a fire that dies down after a few seconds.

Remember to destroy the object after it’s no longer needed otherwise it will add up the game memory and cause the game to freeze. So, it’s better not to use instantiate when multiple spawn is required. In that case, you can create an object pool and make them active when required.

For example, you can add a bullet Gameobject to the scene and set it as inactive. When the gun fires set the Vector3 position of the bullet to the front of the gun and make the bullet object active. When the bullet hits a surface then deactivate it rather than destroying it. This will reduce the memory load caused by the instantiate function. You can learn about colliders to know when to deactivate the Gameobject.

You instantiate wisely to improve game performance. If you have any other questions, leave it in the comment below.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.