Having some issues with a Level Array?

Hello,

so I have this level array script but I just could figure out how to add more tiles. When I got it to work it placed every tiles twice as much. I know it has something to with the else statement but I just can’t figure out what it is, because when I only copy the if and not the else with it, it still makes twice as much tiles. Already thanks for your help

var blockPiece1 : GameObject;
var blockPiece2 : GameObject;

var levelArray:int[] = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                        1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
                        1,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,0,0,0,1,
                        1,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,1,0,0,0,0,0,0,0,0,1,
                        1,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,0,0,1,
                        1,0,0,0,0,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,
                        1,0,0,0,1,1,1,1,1,0,1,1,0,1,1,1,0,0,0,1,
                        1,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,1,
                        1,0,0,0,1,1,1,1,1,0,0,1,1,0,1,1,0,0,0,1,
                        1,0,0,0,0,1,1,1,1,1,0,1,1,1,1,0,0,0,0,1,
                        1,0,0,0,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,1,
                        1,0,0,0,0,0,1,1,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,1,0,0,0,1,
                        1,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,0,0,0,1,
                        1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                        1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
                        1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
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);
            	pieces[x,z].name = "tiles";

        }
    }
}

You have declared a a 1D array, but are indexing it as a 2D array using this logic:

z*20 + x

‘20’ is the length (height) of the 2D array. A better programming practice would be to set it to the length:

  if(levelArray[z*length + x] == 1)

After making this change, you need to add whole rows or columns to the data and make the appropriate change to the ‘width’ and ‘length’ variables. Since they are public, you need to make the change in the Inspector, not in the code.