Instantiate Prefabs At Run Time In Unity 3D

Introduction

 
This article demonstrates the basics of how to instantiate prefabs at run time in Unity.
 
Prerequisites
 
Unity Environment version 2018.4.19f1
 
Create the project
 
Instantiate Prefabs At Run Time In Unity 3D
 
First, you have to open the Unity project. Click on the GameObject in the menu bar. Select 3D object and pick the Empty GameObject.
 
Empty GameObject will be added in your Scene View. 
 
Instantiate Prefabs At Run Time In Unity 3D
 
Instantiate Prefabs At Run Time In Unity 3D
Create Sphere
 
Click on the GameObject in the menu bar. Select 3D object and pick the Sphere.
 
Instantiate Prefabs At Run Time In Unity 3D
 
Create Cube
 
Click on the GameObject in the menu bar. Select 3D object and pick the Cube.
 
Instantiate Prefabs At Run Time In Unity 3DInstantiate Prefabs At Run Time In Unity 3D
 
Create Plane
 
Click on the GameObject in the menu bar. Select 3D object and pick the Plane.
 
Instantiate Prefabs At Run Time In Unity 3D
The cube will be added in your Scene View. Drag and drop the red material into the cube. The color of the cube will now be changed into blue. Set the cube like in the following image.
 
Instantiate Prefabs At Run Time In Unity 3D
 
Sphere will be added. Drag and drop the light green material into the sphere. The sphere will change its color into light green.
 
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
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 Scripts
 
Right click in Create. Select C# Scripts.
 
Instantiate Prefabs At Run Time In Unity 3D
 
Go to the mono development in Unity and write the coding like the following.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class CircleFormat: MonoBehaviour {  
  5.     public GameObject myPrefab1;  
  6.     public GameObject myPrefab2;  
  7.     public GameObject myPrefab3;  
  8.     void Start() {}  
  9.     // Update is called once per frame  
  10.     void Update() {  
  11.         Instantiate(myPrefab1, new Vector3(0, 0, 0), Quaternion.identity);  
  12.         Instantiate(myPrefab2, new Vector3(5, 3, 5), Quaternion.identity);  
  13.         Instantiate(myPrefab3, new Vector3(25, 50, 75), Quaternion.identity);  
  14.     }  
  15. }   
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.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class CircleFormat: MonoBehaviour {  
  5.     public GameObject myPrefab1;  
  6.     public GameObject myPrefab2;  
  7.     public GameObject myPrefab3;  
  8.     public GameObject prefab1, prefab2, prefab3;  
  9.     public int numberOfObjects = 20;  
  10.     public int numberOfObjects2 = 40;  
  11.     public float radius = 5 f;  
  12.     public float radius1 = 10 f;  
  13.     // Start is called before the first frame update  
  14.     void Start() {  
  15.         for (int i = 0; i < numberOfObjects; i++) {  
  16.             float angle = i * Mathf.PI * 2 / numberOfObjects;  
  17.             float x = Mathf.Sin(angle) * radius;  
  18.             float z = Mathf.Sin(angle) * radius;  
  19.             Vector3 pos = transform.position + new Vector3(x, 0, z);  
  20.             float angleDegrees = -angle * Mathf.Rad2Deg;  
  21.             Quaternion rot = Quaternion.Euler(0, angleDegrees, 0);  
  22.             Instantiate(prefab1, pos, rot);  
  23.         }  
  24.         for (int i = 0; i < numberOfObjects; i++) {  
  25.             float angle = i * Mathf.PI * 2 / numberOfObjects;  
  26.             float x = Mathf.Cos(angle) * radius;  
  27.             float z = Mathf.Sin(angle) * radius;  
  28.             Vector3 pos = transform.position + new Vector3(x, 0, z);  
  29.             float angleDegrees = -angle * Mathf.Rad2Deg;  
  30.             Quaternion rot = Quaternion.Euler(60, angleDegrees, 60);  
  31.             Instantiate(prefab2, pos, rot);  
  32.         }  
  33.         for (int i = 0; i < numberOfObjects2; i++) {  
  34.             float angle = i * Mathf.PI * 2 / numberOfObjects2;  
  35.             float x = Mathf.Cos(angle) * radius1;  
  36.             float z = Mathf.Sin(angle) * radius1;  
  37.             Vector3 pos = transform.position + new Vector3(x, 0, z);  
  38.             float angleDegrees = -angle * Mathf.Rad2Deg;  
  39.             Quaternion rot = Quaternion.Euler(30, angleDegrees, 60);  
  40.             Instantiate(prefab3, pos, rot);  
  41.         }  
  42.         // Update is called once per frame  
  43.         void Update() {  
  44.             Instantiate(myPrefab1, new Vector3(0, 0, 0), Quaternion.identity);  
  45.             Instantiate(myPrefab2, new Vector3(5, 3, 5), Quaternion.identity);  
  46.             Instantiate(myPrefab3, new Vector3(25, 50, 75), Quaternion.identity);  
  47.         }  
  48.     }  
  49. }   
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 understood how to  instantiate prefabs at run time in Unity.