- Home /
Question by
Nelis · Mar 09, 2013 at 01:48 PM ·
2dgameobjectarraycoordinates
2D gameobject array coordinates
Hello, I'm trying to set each gameobject to his coordinates. Like the object at (0,0) is Gameobject0-0, and so on. I'm making this so when i press the down button, i can find the gameobject at my x pos rounded and my y pos rounded - 1. Can someone please help me, or give me a hint how i can make this? Thank you -Nelis var prefab : GameObject;
var gridX = 5;
var gridY = 5;
private var spacing = 1;
var SpawnX;
var SpawnY;
function Start ()
{
for (var y = 0; y < gridY; y++){
for (var x = 0; x < gridX; x++){
var pos = Vector2(x,y) * spacing;
Instantiate(prefab, pos, Quaternion.identity);
}
}
}
Comment
Best Answer
Answer by Kiloblargh · Mar 09, 2013 at 05:36 PM
ok, that's easy enough, FTFY.
haven't tested, but should either work or almost work.
var grid2D : GameObject[,];
var gridX = 5;
var gridY = 5;
private var spacing = 1;
var SpawnX;
var SpawnY;
function Start ()
{
grid2D = new GameObject[gridX,gridY];
for (var y = 0; y < gridY; y++){
for (var x = 0; x < gridX; x++){
var pos : Vector3 = Vector3(x,y,0) * spacing; // you can't instantiate at a Vector2 location!
var newBlock : GameObject = Instantiate(prefab, pos, Quaternion.identity);
grid2D[x,y] = newBlock;
}
}
Debug.Log (grid2D[3,3].transform.position);
}