How to spawn an object using Instantiate in Unity

February 13, 2024

Spawning refers to the creation of new game objects during runtime. In Unity, it is very easy to spawn an object using the Instantiate function.

In this article, you will learn how to use the Instantiate function to spawn objects in Unity.

How to Instantiate an Object in Unity

Unity has a built-in Object class which acts as the base class for all objects in the Unity editor.

The Instantiate function is part of the Object class that allows you to spawn new objects in your Scene during runtime. The Instantiate function duplicates an existing object and returns the clone. It has different parameters.

  • original: The existing object you want to clone.
  • position: cloned object position. The default value is the position of the original object.
  • rotation: cloned object orientation. The default value is the orientation of the original object.
  • parent: the parent to be attached to the clone.
  • instantiateInWorldSpace: True to position the clone directly in world space. False to set the clone’s position relative to its parent.

It’s only the original parameter that has to be defined; other parameters have default values.

To instantiate an object from a script:

using UnityEngine;

public class InstantiateExample : MonoBehaviour
{
  public GameObject objectToInstantiate; // Assign the object you want to instantiate

  void Start()
  {
    Instantiate(objectToInstantiate)
  }
}

In the above code, objectToInstantiate is the game object you want to instantiate. It returns the cloned object with default values for position and rotation.

You can also define the position and rotation parameters.

Instantiate(objectToInstantiate, Vector3.zero, Quaternion.identity);

This instantiates the object at a specific position and rotation. This is just an example code; you should use your own position and rotation values.

In some cases, you may want to instantiate a Prefab. A Prefab is a saved instance of a GameObject that is reusable in your game scenes.

How to create a Prefab

Before you can instantiate a Prefab, you need to know how to create it.

To create a Prefab in Unity;

  • First, you need to have a GameObject in your scene that you want to turn into a Prefab.
  • Locate the GameObject in the Hierarchy window.
  • Click and drag the GameObject onto an empty area in the Project window.
  • Doing this, Unity will create a new Prefab with the same name as the Game Object.

Your newly created Prefab will appear in your project’s assets.

You can now pass the Prefab into the Instantiate function to create a new instance of the Prefab.

Instantiate(newPrefab); //instantiating the newly created Prefab

How to Instantiate Multiple Objects in Unity

You can use a for loop to create several instances of a GameObject or different GameObjects in Unity. Like this:

using UnityEngine;

public class InstantiateMultiple: MonoBehaviour

{
  public GameObject objectToInstantiate;
  public int numberOfObjects = 6;

  void Start()

  {
    for (int i = 0; i < numberOfObjects; i++) //loop through the number of objects to instantiate
    {
      // calculate position for each object
      Vector3 position = new Vector3(i * 2, 0, 0);

      //no rotation
      Quaternion rotation = Quaternion.identity; 

      //instantiate the object
      GameObject newObj = Instantiate(objectToInstantiate, position, rotation); 
    }
  }
}

In the script above, a for loop is used to create six instances of a GameObject with a calculated position and rotation.

Now, you have learned how to use the Instantiate function to spawn objects in Unity. You can visit the Unity Docs to learn more about the Instantiate function.

Want to build better games?

© 2024