- Home /
Question by
leooon12 · Mar 21, 2012 at 03:20 PM ·
2djavascriptarray
Need help with 2D Arrays (problems with navigating)
Hello everyone! Sorry for my bad English :)
I'm trying to understand logic of 2d arrays and make a grid for a turn-based game. After making an array and generating prefabs, no tiles are highlighted with red. What can I do to highlight a tile I want? This tile must be the tile right in the middle of my map. My code:
#pragma strict
var tiles : GameObject[,] = new GameObject [32,32];
var TilePrefab : GameObject;
var x : int = 32;
var z : int = 32;
function Start () {
for (var i = 0; i < z; i++)
for (var j = 0; j < x; j++) {
Instantiate (TilePrefab, Vector3(j*10, 0, i*10), Quaternion.identity);
tiles[j,i] = TilePrefab;
}
tiles[16,16].renderer.material.color = Color.red;
print("It is working");
}
Comment
Best Answer
Answer by Eric5h5 · Mar 21, 2012 at 03:37 PM
You're assigning all of the tiles to the prefab, rather than assigning each tile to an instance of the prefab.
Thank you, that solved my problem:
tiles[j,i] = Instantiate (TilePrefab, Vector3(j*10, 0, i*10), Quaternion.identity);