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."
You must first open the Unity project. In the menu bar, click the GameObject. Choose Create Empty from the list of 3D objects.
Your Scene View will now include an empty GameObject.
Create Sphere
Click the GameObject.Choose the 3D Object and pick the Sphere.
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.
In the scene view, the Cube Object will be visible. The name appears in the Hierarchy view.
Create Plane
Click the GameObject.Choose the 3D Object and pick the Plane.
In the scene view, the Plane Object will be visible. The name appears in the Hierarchy view.
Create Material
Click the GameObject.Choose Create and pick the 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.
A sphere will be added. The Sphere will change its color to light green. Drag and drop the light green Material into the Sphere.
Click on the Sphere in the left menu. Go to "Add Component" and click on Rigidbody, as shown below.
Go to Rigidbody and check the "Use Gravity" option.
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.
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.
Go back to the Unity window. Drag and drop the Circleformat script onto the GameObject.
Click on the Play button. The Sphere will Instantiate Prefabs at run time.
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.
Summary
I hope you understand how to Instantiate prefabs at run time in Unity.