- Home /
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";
}
}
}
Answer by robertbu · Apr 01, 2013 at 01:57 PM
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.
This does fix the resizing of the level, thanks. But I actually was looking for a way to add more GameObject to the array. At this moment I am using a the 0 as floor and the 1 as collider (tree) but I would like to add a 2 ect, etc but I can't get it to work.
what about using:
var tileType = levelArray[z*20 + x];
if (tileType == 1)
prefab = blockPiece1;
else if (tileType == 2)
prefab = blockPiece2;
else if (tileType == 3)
prefab = blockPiece4;
else if (tileType == 4)
prefab = blockPiece4;
without
pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);
the blockPieces won't be placed, but whenever I try to add it, else if won't work
Can you explain why adding else if won't work?
I don't believe Next Beat Games was saying to get rid of pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);
but, if you put it after this if/else if/etc block it seems like it should work, since all that line cares about is prefab.
It's working, at the end of all the else if, add the else with the Instantiate. Thank you so much
if (tileType == 1) //Tile 1
prefab = blockPiece1;
else if (tileType == 2) // Tile 2
prefab = blockPiece3;
else
prefab = blockPiece2; //Floor
pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);