How can I create a grid of transforms?

I want to procedurally create a grid of transforms as large as I specify, I do know that I will be using a loop however I don’t fully understand loops. I just need someone to write some psuedo code and give an explanation and I can go from there.

Edited code: This should be what you want… you have to initialize prefab to the object you are creating… Either by simply drag and drop in the inspector, or by scripting if necessary.

This could be done in the Start function:

function Start(){
prefab = GameObject.FindWithTag("grid object"); // for example

As for setting the name, probably not necessary, you can access them all via the array, as in:

my2DArray[x,y] (where x and y are the grid coordinates, from 0 to 9 in this case…)

you create a two-dimensional array:

var x = 10;
var y = 10;
var spacing : float = 10;
var prefab : GameObject;

var my2DArray : GameObject[,] = new GameObject[x,y];

function Start{

for(var i = 0;i < x;i++)
for(var j = 0;j < y;j++)

my2DArray[i,j] = Instantiate(prefab, Vector3(j * spacing,i * spacing,0), transform.rotation);



by nesting the loops this way, it goes through the whole thing
hope this helps


to clarify-

for(var i = 0;i < x;i++)

the first part (var i = 0;) is the var you will use to increment through the loop

the second (i < x;) is the condition which,when no longer met, will end the loop

the third (i++;) is the incrementation of the var

so, we start with i, which is equal to 0. Each pass, we do i++ ( i + 1 ). When i is no longer less than x (which is 10), the loop ends (does not get entered if condition is not met)

once the inner loop completes a full cycle, it ends, causing the outer loop to increment by 1, then re-enter the inner loop… if that makes sense

// im assuming that you need to generate in xz plane…

var ObjectToSpawn : Transform;

function GenerateTransforms(xLength:int,zLength:int,startPosition:Vector3)

  for(var i=0;i<xLength;i++)           // switches to the next column of z, continues until xLength times it has switched.
    for(var j=0;j<zLength;j++)         // generates zLength number of transforms along z axis.
      var temp:GameObject = (GameObject) Instantiate(ObjectToSpawn,
                       Vector3(startPosition.x+ObjectToSpawn.localScale.x*i/*+offset if any*/,           
                               startPosition.z+ObjectToSpawn.localScale.y*j/*+offset if any*/),

         //places the object according the start position.



what we are trying to work out with the grid is an AI system, the idea is to set up a grid so that when the player clicks a point to move a unit to the computer can move it in a non stupid way. like say the unit is in D5 and the player clicks in A3 the unit would move and when it hit a trigger to change directions it looks at 2 arrays to know what way to go. in other words if it needed to turn left or right the arrays would say have letters and say left had A B C and right had E F G it would check see A in the 1st one and know to go left same for up and down 1 2 3 4 and the 2nd was 6 7 8 9 then it would check see 3 in the 1st one and know to go up. maybe not the best way to do something like this but its the idea i had