I’m using the below code to spawn a simple grid of objects. It works but only spawns vertically. How can I modify this to spawn everything horizontally instead?
void Start()
{
for(int i = 0; i < columnLength * rowLength; i++){
Instantiate(prefab, new Vector3(x_Start + (x_Space * (i % columnLength)), y_Start + (-y_Space * (i / columnLength))), Quaternion.identity);
}
}
It’s because you are instantiating on the X and Y axes. Unity defines Y as vertical. Z is into the screen (the default forward direction). You really want to instantiate on (x, 0, z), not (x, y, 0);
void Start()
{
for (int i = 0; i < columnLength * rowLength; i++)
{
float x = x_Start + (x_Space * (i / columnLength));
float z = z_Start + (-z_Space * (i % columnLength));
Instantiate(prefab, new Vector3(x, 0, z), Quaternion.identity); ;
}
}
Edit: And because I’m a stickler for self-documenting code, I’ve changed the operators in the calculation of x and Z. Conventionally speaking, rows go left to right (along the X-axis). Columns go either up or away from you. Feel free to ignore this change but code is read and re-read and it’s best to be as clear as possible.