U
tab 01Basics11 lessons — tap to open
08 / 11 · 9 min
Basics

Spawn a grid from code

your project so far

Assets/

Scenes/

Arena.unity

FloorBlocks (parent)

Scripts/

GridSpawner.cs

Prefabs/

P_Cube

you already havethis lesson adds

Placing 100 cubes by hand is a waste of an afternoon. Two nested loops and one Instantiate call do it in a frame.

animated diagram

x runs across, z runs into the screen, and one cube drops per cell.

The grid spawner

GridSpawner.cs
using UnityEngine;

public class GridSpawner : MonoBehaviour
{
    [SerializeField] private GameObject blockPrefab;
    [SerializeField] private int columns = 6;      // along X
    [SerializeField] private int rows = 6;
    [SerializeField] private float spacing = 1.5f; // metres between blocks
    [SerializeField] private Transform parent;

    void Start()
    {
        BuildGrid();
    }

    void BuildGrid()
    {
        // centre the grid on this object
        float offsetX = (columns - 1) * spacing * 0.5f;
        float offsetZ = (rows - 1) * spacing * 0.5f;

        for (int x = 0; x < columns; x++)
        {
            for (int z = 0; z < rows; z++)
            {
                Vector3 pos = new Vector3(
                    x * spacing - offsetX,
                    0.5f,                       // half the cube height
                    z * spacing - offsetZ);

                GameObject block = Instantiate(blockPrefab, pos, Quaternion.identity, parent);
                block.name = $"Block_{x}_{z}";  // readable in the Hierarchy
            }
        }
    }
}
  1. 1

    Create an empty GameObject, name it Spawner, put it at 0, 0, 0.

  2. 2

    Attach GridSpawner.cs to it and drag P_Cube into the Block Prefab slot.

  3. 3

    Create another empty called Blocks and drag it into the Parent slot — all spawned cubes will nest under it.

  4. 4

    Press Play. 36 cubes appear, centred on the floor.

Read the maths once

  • x * spacing turns the loop counter 0,1,2… into metres: 0, 1.5, 3, …

  • Subtracting offsetX shifts the whole grid left by half its width, so it sits centred instead of starting at the origin.

  • The fourth argument of Instantiate is the parent Transform. Passing it keeps the Hierarchy from turning into 36 loose rows.

Variations worth trying

inside the inner loop
// 1 — random colour per block
block.GetComponent<Renderer>().material.color =
    Random.ColorHSV(0f, 1f, 0.4f, 0.7f, 0.7f, 1f);

// 2 — random height, so it reads like a city
float h = Random.Range(0.5f, 4f);
block.transform.localScale = new Vector3(1f, h, 1f);
block.transform.position   = new Vector3(pos.x, h * 0.5f, pos.z);

// 3 — a wave, using the distance from the centre
float d = new Vector2(x - columns * 0.5f, z - rows * 0.5f).magnitude;
block.transform.position += Vector3.up * Mathf.Sin(d) * 0.6f;

Clearing the grid

C# script
public void ClearGrid()
{
    // backwards: still right if this ever becomes DestroyImmediate
    for (int i = parent.childCount - 1; i >= 0; i--)
        Destroy(parent.GetChild(i).gameObject);
}