Hello! At runtime I am instantiating a bunch of Game Objects and then parenting them to a Game object already present in the game, lets call it “Grid”. I can center the Grid on the screen just fine if it has no children:
Vector3 gridPos = new Vector3 (Screen.width / 2f, Screen.height / 2f, 10f);
grid.transform.position = Camera.main.ScreenToWorldPoint (gridPos);
There, I use the camera to center the object and that works just fine. When the Grid gets populated with children, it still centers fine but it stays the same height and width, so the children objects go off to the right(and off screen). Here’s a picture:
Here’s the function that I use to instantiate the children in the Start() function:
void GenerateSlotLine() {
int counter = 0;
for (float x = 0f; x <= 20; x++ ) {
Transform tile = (Transform) Instantiate ( tile_prefab, new Vector3 (x, 1f, 0f), Quaternion.identity);
tile.name = "Tile" + counter;
tile.parent = grid.transform;
counter++;
}
}
How can I offset the children in such a way that the parent goes off to the left but the children look like they are centered, or perhaps there is another solution like resizing the bounds of the parent? Thank you!