Unity3D – 2D Array – 2D Grid of GameObjects

2D Grid of GameObjects

Create a scene with:

– Sphere
– EmptyGameObject and assign Grid.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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