Introduction to instantiating prefabs
Introductory context and common scenarios in which instantiating prefabs is useful.
Read time 3 minutesLast updated 21 days ago
You can use prefabs to instantiate complicated GameObjects or collections of GameObjects at runtime. Compared with creating GameObjects from scratch using code, instantiating prefabs using code has many advantages because you can do the following:
- Instantiate a prefab using one line of code. Creating equivalent GameObjects from scratch requires many more lines of code.
- Change which prefab is instantiated without changing the code. You can make a simple rocket into a super-charged rocket, without any code changes.
Common scenarios
Instantiating prefabs at runtime is useful in the following common scenarios:
- Building a structure out of a single prefab by replicating it multiple times in different positions, for example in a grid or circle formation.
- Firing a projectile prefab from a launcher. The projectile prefab can be a complex configuration containing a mesh, Rigidbody, collider, audio source, dynamic light, and a child GameObject with its own trail Particle System.
- A vehicle, building, or character, for example a robot, breaking apart into many pieces. In this scenario, the example script deletes and replaces the complete, operational robot prefab with a wrecked robot prefab. This wrecked prefab consists of separate broken parts of the robot, each set up with Rigidbody components and Particle Systems of their own. This technique allows you to blow up a robot into many pieces with just one line of code, which replaces the original GameObject with a wrecked prefab.
Instantiate a prefab
To instantiate a prefab at runtime, your code needs a reference to the prefab. To make this reference, you can create a public field of type in your code, then assign the prefab you want to use to this field in the Inspector.
GameObjectThe following script example has a single public variable, , which is a reference to a prefab. It creates an instance of that prefab in the method.
myPrefabStartusing UnityEngine;public class InstantiationExample : MonoBehaviour { // Reference to the prefab. Drag a prefab into this field in the Inspector. public GameObject myPrefab; // This script will simply instantiate the prefab when the game starts. void Start() { // Instantiate at position (0, 0, 0) and zero rotation. Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity); }}
To use this example:
- Create a new MonoBehaviour script in your Project, and name it .
InstantiationExample - Copy the code and paste it into your new script, and save it.
- Create an empty GameObject using the menu > Create Empty.GameObject
?
- Add the script to the new GameObject as a component by dragging it onto the empty GameObject.
- Create any prefab, and drag it from the into the My Prefab field in the script component.Project window
?
When you enter Play mode, the prefab instantiates at position (0 , 0 , 0) in the scene.
You can drag a different prefab into the My Prefab field in the window to change which prefab Unity instantiates, without having to change the script.
Inspector
?