# Fitting Gameobjects within camera view

I am trying to fit a grid of gameobject into the orthographic view but I have a problem whenever I change the amount of grids to display it would overlap the view.

What cαn I do to fit a series of gameobjects, having always the same margin as the amount of grid changes?

private void Start() {
for(int i = 0; i < wordSearch.Gridsize; i++) {
for(int j = 0; j < wordSearch.Gridsize; j++) {
cell = Instantiate(gridCell, new Vector3(j * 1 * gridCell.transform.position.x * cellSpacing, i * 1 * gridCell.transform.position.y * cellSpacing, 0), Quaternion.identity);
cell.name = "cell-Y: " + i.ToString() + " cell-X: " + j.ToString();
GameObject textObject = cell.transform.GetChild(0).gameObject;
textObject.GetComponent<TextMeshPro>().text = wordSearch.matrix[i, j].ToString();
textObject.name = textObject.GetComponent<TextMeshPro>().text;
}
}
CenterPoint();
// FitToScreen();
}

private void CenterPoint() {
Vector3 offset = new Vector3(0, -1, -10);
cellGameobject = GameObject.FindGameObjectsWithTag("cell");
transformCell = new Transform[cellGameobject.Length];
for(int i = 0; i < cellGameobject.Length; i++) {
transformCell[i] = cellGameobject[i].transform;
}
for(int a = 0; a < transformCell.Length; a++) {
cellVectors += transformCell[a].position;
}
centercell = cellVectors / transformCell.Length;
GetComponent<Camera>().transform.position = centercell + offset;
}

private void FitToScreen() {
Camera.main.orthographicSize = /* Insert Comment */
}

• I dont know the answer for you but i've also struggled in the past with the same thing. Firstly though, I wonder why you multiply by 1 on the Instantiate of a cell ? (Surely that has no effect?) . Also you don't need the .ToString() when you create the cell names because you've already used the string to make "cell-Y" and so simply "cell-Y: " + i + "cell-x ....etc.etc, will give you the same result. Thats just a little sidenote, I cant offer anything much on setting the camera view. I would say that of course the screen-width will obviously need to be set to cell-width * GridSize.x + margin. – Big T Larrity Nov 22 '18 at 12:44
• hope its some help to you whilst waiting on proper answers – Big T Larrity Nov 22 '18 at 12:44
• @SuperMegaBroBro I'll add those to my changes. Thanks for enlightenment. – Shulz Nov 22 '18 at 13:49