Unity Instantiate: How and When to use

When we make games we tend to spawn many characters and object during runtime. In unity, instantiate is the function used for this. This enables us to spawn any game object at any point of the game world. Unity requires the game object to be a 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 a prefab?

Before we jump into instantiate in unity, 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 resource folder for deployment is called a prefab in unity.

How to make a prefab?

Say you have a box that you need to spawn into the game at given point. Add that object into the hierarchy any scene. Now you have to set the behavior of the box by adding the required components. For example, I will want the box to be a kinematic rigidbody and have a box collider added to it.

After adding all required components you can drag the game object from hierarchy back to resource folder. When you drag it back you can see that the symbol is different and all parameters assigned to the game object is retained. You can change the name as desired.That’s it, you have made a prefab.

Or you can just download the required assets from the asset store.

Unity instantiate:How to use?

You can spawn any gameobject using this code

instantiate(gameobject,position,rotation);

The first input takes the name of the gameobject or prefab you want to spawn. Second takes the position you want the object to be in. Third is the rotation you want to apply to the object. If you don’t want to apply rotation to the object then you can specify the third input as “quanternion.identity”.

A common mistake many beginner’s make is they attach the Instantiate script to an object which is not active. You need to attach the script to any gameobject which is active in the scene.

Unity instantiate: sample script.

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);
}
}

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. Lets see what are the ideal conditions when you can use instantiate.

  1. Spawn single characters that stay through out 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 fire that die down after a few seconds.

Remember to destroy the object after its no longer needed otherwise they will add up the game memory and cause the game to freeze. So its 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 scene and set it as inactive. When the gun fires set the position of bullet to front of the gun and make the bullet object active. When the bullet hits a surface then deactivate it rather than destroying. This will reduce the memory load caused by the instantiate function. You can learn about colliders to understand the requirement of when to deactivate the gameobject.

You instantiate wisely to improve game performance.

Leave a Reply

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