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

Difficulty Beginner

Objectives:

• 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

Tutorial:

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:

###### 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:

###### // 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 won‘t 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.