Change postion of the camera to postion of an object

I made a small script which spawns grid of cubes.

Inside this is an IF which checks for position of first cube, so that I could focus camera on it. But since object is a clone I don’t know how to get the postion, I wrote the code there is no errors but nothing happens. Here is the code:

``````var tilePrefab : GameObject;
``````

var y : int = 10;
var x : int = 10;
var postionCameraX : int;
var postionCameraY : int;
var postionCameraZ : int;
function Start () {

``````for (var i : int = 0;i < y; i++) {
for (var j : int = 0; j < x; j++)
{
var tile : GameObject = Instantiate (tilePrefab, Vector3(j , i, 0), Quaternion.identity);
if (i == 0 && j == 0)
{

postionCameraX = tile.transform.position.x;
postionCameraY = tile.transform.position.y;
postionCameraZ = tile.transform.position.z;
Camera.main.transform.position.x = postionCameraX;
Camera.main.transform.position.y = postionCameraY;
}
};
};
``````

Also can someone tell me if I can somehow get whole Vector3 in a variable so I don’t need to have 3 line to get x,y,z with tile.transform positon.x… ?

You can assign any Vector3 value or variable to transform.position; instead of using 3 variables, you could have a single Vector3 variable, generate the desired position and assign it to the camera position:

```var tilePrefab : GameObject;
var y : int = 10;
var x : int = 10;

function Start (){
for (var i : int = 0;i < y; i++) {
for (var j : int = 0; j < x; j++)
{
var tile : GameObject = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);
if (i == 0 && j == 0)
{
var pos: Vector3 = tile.transform.position; // get the tile position...
pos.z = Camera.main.transform.position.z; // but keep the camera z
Camera.main.transform.position = pos; // move camera to the position
}
}
}
}
```

Thank you.