Quad or Objects grid creation for unity 3D tutorial C#

Difficulty Beginner


  • Create a grid of quad objects in unity 3D
  • modify the grid’s columns and rows count
  • modify the separation between each grid unit in two axis
  • modify the script to be able to use prefabs instead of just quads


Lets Start Off by creating a C# script: right click in the project window and choose Create->C# Script just like in the image Below


now lets rename the script: GridScript

Double click the script to open it in monodevelop or your assigned IDE we get something like the script below.


now lets start by creating our variables:

Above void Start write the following:

public int NumberOfColumns = 10; // number of columns for the grid
public int NumberOfRows = 10; // number of rows for the grid
public float SeperationValueX = 0.0f; // Distance between each column
public float SeperationValueZ = 0.0f; // Distance between each Row

private float tempSepX = 0; // used to calculate the separation between each column
private float tempSepZ = 0;// used to calculate the separation between each row

now that we created the variables we need we will start by creating the function that will put them to good use.

After the Update function lets create a new function named create Grid:

void createGrid()

to create the grid we need a double loop, one for the columns and one for the rows, we also need to create and position quads game objects so lets do this next, add the following code to the create grid function and lets call it from the Start function:

void createGrid()
      for (int i = 0; i < NumberOfColumns; i++) {//loop 1 to loop through columns
            for (int j = 0; j < NumberOfRows; j++) {//loop 2 to loop through rows
                GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Quad); //create a quad primitive as provided by unity
                plane.transform.position = new Vector3(i, 0,j); //position the newly created quad accordingly

    // Use this for initialization
    void Start () {
        createGrid ();//call the createGrid function on start

now attach this script to any game object in the scene and hit play in unity you should get something like the image below


you can notice that we created a grid, but something is wrong, all the quads are rotated, to fix that we need to add a line of code inside the CreateGrid script just after “plane.transform.position” line:

plane.transform.eulerAngles = new Vector3 (90f,0,0); //rotate the quads 90 degrees in X to face up

now save the script and hit play in unity, you should have a fully flat grid like you expect it.


PS: changing the values of NumberOfRows and NumberOfColumns will create a different grid size as expected.

now lets add a special function to the grid script to be able to create seperations between each piece

lets modify the createGrid function as follows:

void createGrid()
        for (int i = 0; i < NumberOfColumns; i++) {//loop 1 to loop through columns
            for (int j = 0; j < NumberOfRows; j++) {//loop 2 to loop through rows
                GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Quad); //create a quad primitive as provided by unity
                plane.transform.position = new Vector3(i+tempSepX, 0,j+tempSepZ); //position the newly created quad accordingly
                plane.transform.eulerAngles = new Vector3 (90f,0,0); //rotate the quads 90 degrees in X to face up
                tempSepZ += SeperationValueZ;//change the value of seperation between rows
            tempSepX += SeperationValueX;//change the value of seperation between columns
            tempSepZ = 0;//Reset the value of the seperation between the rows so it wont cumulate

now you can create separated grid units.


the final script should look like the image below


How to modify the script to use any game object (prefabs)?

add this variable at the top

    Public GameObject TheGridItem; // this is the item u will use

also remove lines 28 -29 -30 in the image above and replace them with this line of code:

Instantiate(TheGridItem, new Vector3(i+tempSepX, 0, j+tempSepZ), Quaternion.identity);

and you are done.

hope you enjoyed this tutorial, if you have any question don’t hesitate to ask.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: