Unity3D – 2D Array – 2D Grid of GameObjects

2D Grid of GameObjects

Create a scene with:

– Sphere
– EmptyGameObject and assign Grid.js:


#pragma strict
 
var  oggetto:GameObject; // Assign in Inspector
var  griglia:GameObject[,];
 
function Start () {
 
	griglia = new GameObject[5,5];// 
	 
	for (var i=0;i<5;i++)
	 
	{
	   
	    for(var j=0;j<5;j++)
	    {
	 
	    griglia[i,j] = oggetto;
	    Instantiate(griglia[i,j],Vector3(i,0,j),Quaternion.identity); // Add 1 units 
	    print(griglia);
	   
	    }
	 
	}
}// END Start()

DRAG AND DROP Sphere over the slot of var oggetto

Play

You will see a grid of Spheres of 5×5 units, the pattern is:

OOOOO
OOOOO
OOOOO
OOOOO
OOOOO