Build a structure with prefabs
Use prefabs to instantiate several copies of a block prefab to construct a wall.
Read time 2 minutesLast updated 18 days ago
You can use code to procedurally generate copies of a prefab in a particular configuration. The following example creates a wall of block prefab instances.
Create a block prefab asset
To create a wall of blocks, you must first have a block prefab asset to create the wall with.
- In the Hierarchy, right-click and select > Cube.3D Object
?
- Drag the cube into the Assets folder in the Project window. Unity creates a prefab asset of the cube.
- Rename the prefab asset to .
Block - Delete the original cube asset from the Hierarchy.
Create a script that instantiates blocks
Create a MonoBehaviour script and name it . Add the following code into the script:
WallWallusing UnityEngine;public class Wall : MonoBehaviour{ public GameObject block; public int width = 10; public int height = 4; void Start() { for (int y=0; y<height; ++y) { for (int x=0; x<width; ++x) { Vector3 pos = transform.position + new Vector3(x, y, 0); Instantiate(block, pos, Quaternion.identity); } } }}
Attach the script to an empty GameObject
- Create an empty GameObject. Right-click on the Hierarchy and select Create Empty.
- Attach the script to the empty GameObject.
Wall - Drag the prefab asset into the Block field,
Block - Enter Play mode and Unity generates a wall with the prefab.
Block

A wall built from 4 rows of 10 blocks. The empty GameObject is selected in the Inspector, with the Block prefab asset assigned to the Block field.
You can also use code to place prefabs in a grid or other configurations. The following example places the cubes in a circular formation:
using UnityEngine;public class CircleFormation : MonoBehaviour{ // Instantiates prefabs in a circle formation public GameObject prefab; public int numberOfObjects = 20; public float radius = 5f; void Start() { 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(0, angleDegrees, 0); Instantiate(prefab, pos, rot); } }}
When you attach the script to an empty GameObject and enter Play mode, Unity generates the cubes in the following formation:

A circular arrangement of blocks.