- Home /
Question by
Gng357 · May 07, 2019 at 02:08 PM ·
gameobjectpositioningintantiate
Wrong positioning of game objects after instantiating them
I instantiate a Board GameObject using this code:
mainBoard.transform.position = camera.ViewportToWorldPoint(new Vector3(0f, 0f, camera.nearClipPlane));
Then I try to fill this board by instantiating Tiles GameObjects with loop:
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Vector3 tempPosition = camera.ViewportToWorldPoint(new Vector3(x, y, camera.nearClipPlane));
GameObject backgroundTile = Instantiate(tilePrefab, tempPosition, Quaternion.identity) as GameObject;
backgroundTile.transform.SetParent(this.transform);
backgroundTile.name = "( " + x + "," + y + ")";
allTiles[x, y] = backgroundTile;
}
}
But the tiles are placed on wrong place. They should be placed one next to each other at the bottom left of the screen but only the first Tile is at correct position.
How can I get all tiles correctly placed on the board?
Thank you.
Comment