Instantiate Prefabs At Run Time In Unity 3D

Introduction

When you need to run-time instantiate complex GameObjects or sets of GameObjects, prefabs are incredibly helpful. When compared to writing code from scratch to create GameObjects, Instantiate Prefabs has many benefits because you can:

  • Create a Prefab using just one line of code. More lines of code are needed to create equivalent GameObjects from scratch.
  • You can quickly and easily set up, test, and edit the Prefab using the Scene view, Hierarchy, and Inspector.

Prerequisites

Unity Environment version 2018.4.19f1

Make a new Project and name it "Instantiating Prefabs."

Instantiate Prefabs At Run Time In Unity 3D

You must first open the Unity project. In the menu bar, click the GameObject. Choose Create Empty from the list of 3D objects.

Instantiate Prefabs At Run Time In Unity 3D

Your Scene View will now include an empty GameObject.

Create Sphere

Click the GameObject.Choose the 3D Object and pick the Sphere.

Instantiate Prefabs At Run Time In Unity 3D

In the scene view, the Sphere Object will be visible. The name appears in the Hierarchy view.

Create Cube

Click the GameObject.Choose the 3D Object and pick the Cube.

Instantiate Prefabs At Run Time In Unity 3D

In the scene view, the Cube Object will be visible. The name appears in the Hierarchy view.

Create Cube

Create Plane

Click the GameObject.Choose the 3D Object and pick the Plane.

Instantiate Prefabs At Run Time In Unity 3D

In the scene view, the Plane Object will be visible. The name appears in the Hierarchy view.

Plane

Create Material

Click the GameObject.Choose Create and pick the Material.

Create material

The Cube will be added to your Scene View. Drag and drop the red Material into the Cube. The color of the Cube will now be changed to blue. Set the Cube like in the following image.

Instantiate Prefabs At Run Time In Unity 3D

A sphere will be added. The Sphere will change its color to light green. Drag and drop the light green Material into the Sphere.

Instantiate Prefabs At Run Time In Unity 3D

Click on the Sphere in the left menu. Go to "Add Component" and click on Rigidbody, as shown below.

Instantiate Prefabs At Run Time In Unity 3D

Go to Rigidbody and check the "Use Gravity" option.

Instantiate Prefabs At Run Time In Unity 3D

Create Script

You can create a new script using the Create menu at the top left of the Project panel or by going to Assets > Create > C# Script from the main menu.

Instantiate Prefabs At Run Time In Unity 3D

Go to the mono development in Unity and write the coding like the following.

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  
public class CircleFormat: MonoBehaviour {  
    public GameObject myPrefab1;  
    public GameObject myPrefab2;  
    public GameObject myPrefab3;  
    void Start() {}  
    // Update is called once per frame  
    void Update() {  
        Instantiate(myPrefab1, new Vector3(0, 0, 0), Quaternion.identity);  
        Instantiate(myPrefab2, new Vector3(5, 3, 5), Quaternion.identity);  
        Instantiate(myPrefab3, new Vector3(25, 50, 75), Quaternion.identity);  
    }  
}   

Save the program.

Instantiate Prefabs At Run Time In Unity 3D

Go back to the Unity window. Drag and drop the Circleformat script onto the GameObject.

Instantiate Prefabs At Run Time In Unity 3D

Click on the Play button. The Sphere will Instantiate Prefabs at run time.

Instantiate Prefabs At Run Time In Unity 3D

Go to the mono development in Unity and write the code like the following.

using System.Collections;  
using System.Collections.Generic;  
using UnityEngine;  
public class CircleFormat: MonoBehaviour {  
    public GameObject myPrefab1;  
    public GameObject myPrefab2;  
    public GameObject myPrefab3;  
    public GameObject prefab1, prefab2, prefab3;  
    public int numberOfObjects = 20;  
    public int numberOfObjects2 = 40;  
    public float radius = 5 f;  
    public float radius1 = 10 f;  
    // Start is called before the first frame update  
    void Start() {  
        for (int i = 0; i < numberOfObjects; i++) {  
            float angle = i * Mathf.PI * 2 / numberOfObjects;  
            float x = Mathf.Sin(angle) * radius;  
            float z = Mathf.Sin(angle) * radius;  
            Vector3 pos = transform.position + new Vector3(x, 0, z);  
            float angleDegrees = -angle * Mathf.Rad2Deg;  
            Quaternion rot = Quaternion.Euler(0, angleDegrees, 0);  
            Instantiate(prefab1, pos, rot);  
        }  
        for (int i = 0; i < numberOfObjects; i++) {  
            float angle = i * Mathf.PI * 2 / numberOfObjects;  
            float x = Mathf.Cos(angle) * radius;  
            float z = Mathf.Sin(angle) * radius;  
            Vector3 pos = transform.position + new Vector3(x, 0, z);  
            float angleDegrees = -angle * Mathf.Rad2Deg;  
            Quaternion rot = Quaternion.Euler(60, angleDegrees, 60);  
            Instantiate(prefab2, pos, rot);  
        }  
        for (int i = 0; i < numberOfObjects2; i++) {  
            float angle = i * Mathf.PI * 2 / numberOfObjects2;  
            float x = Mathf.Cos(angle) * radius1;  
            float z = Mathf.Sin(angle) * radius1;  
            Vector3 pos = transform.position + new Vector3(x, 0, z);  
            float angleDegrees = -angle * Mathf.Rad2Deg;  
            Quaternion rot = Quaternion.Euler(30, angleDegrees, 60);  
            Instantiate(prefab3, pos, rot);  
        }  
        // Update is called once per frame  
        void Update() {  
            Instantiate(myPrefab1, new Vector3(0, 0, 0), Quaternion.identity);  
            Instantiate(myPrefab2, new Vector3(5, 3, 5), Quaternion.identity);  
            Instantiate(myPrefab3, new Vector3(25, 50, 75), Quaternion.identity);  
        }  
    }  
}   

Save the program.

Come back to the Unity window. Click on the "Play" button. The Cube and Sphere will be instantiated prefabs at run time.

Instantiate Prefabs At Run Time In Unity 3D

Summary

I hope you understand how to Instantiate prefabs at run time in Unity.


Similar Articles