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";
}
}
}
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.
– Pearbookwhat 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;
– Next_Beat_Gameswithout 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
– PearbookCan 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.
– UbahsIt'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);
– Pearbook