Creating a 2d array of gameObjects

Hi there,

I know this question has been asked a few time before but I can’t find an efficient solution to my problem. I want to create a 2.5D isometric game in Unity. I need to create a grid of gameObjects that will be my level.

At the moment I have this code.

var blockPiece1:GameObject;
var blockPiece2:GameObject;
	
var levelArray:Array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
						0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
						0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
						0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
						0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
						0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
						0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
						0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

function Awake() {
	
	for(x=0; x<20; x++){
		for(z=0; z<20; z++){
		print("x: " + x + " z: " + z + " type: " + levelArray[z*20 + x]);
		if(levelArray[z*20 + x] == 1){
		     newPiece = Instantiate (blockPiece1, Vector3(x,1,z), Quaternion.identity);
		} else {
			newPiece2 = Instantiate (blockPiece2, Vector3(x,1,z), Quaternion.identity);
			}
		}
	}
}

This works but seems to be a very inefficient way of creating the Array. I need to be able to click objects and change their type within some kind of Array.

You can create a bidimensional GameObject array and fill it when creating your pieces. Later, at Update, you can access each object using this array - something like obj = pieces[row,col];:

EDITED: Included @Eric5h5 suggestion - changed levelArray to an int array - it’s much more efficient than the Array class, which is untyped and requires a lot of system resources (memory and CPU time). It makes negligible difference in this script, but may serve as an useful example in other cases.

var blockPiece1:GameObject;
var blockPiece2:GameObject;
var levelArray:int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
                        0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
                        0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
                        0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
                        0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
                        0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
                        0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
                        0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var width = 20;
var length = 20;
var pieces: GameObject[ , ];

function Awake() {
    pieces = new GameObject[width, length]; // creates the object array
    for(x=0; x<width; x++){
        for(z=0; z<length; z++){
            print("x: " + x + " z: " + z + " type: " + levelArray[z*20 + x]);
            var prefab: GameObject;
            if(levelArray[z*20 + x] == 1)
                prefab = blockPiece1;
            else
                prefab = blockPiece2;
            pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);
            // you can also add a significant name to each piece:
            pieces[x,z].name = "Piece"+x.ToString("D2")+z.ToString("D2");
        }
    }
}

Sorry, i was trying to use this script, but im not able to reformulate this to add more prefabs, i try a few way but i can´t figure it the right one. If anyone knows how i will apreciate a lot your help.