I’m trying to use this for loop to instantiate tiles that are a single unit wide. The idea is
for each x between 0 and 5, the position will be x, 0, 0. So the first iteration the x would equal 0, the second iteration it would equal 1, and so on until the last object is placed at 5, 0, 0. However, it’s not changing the x parameter for the objects so they all spawn on top of each other at 0, 0, 0. How can I fix this?
I’m hoping to have a button that the player can press that will spawn tiles in a x by z grid, like 5 by 5, or 5 by 10,so the plan was to use nested for loops. to instantiate the tiles at the correct positions.
Alternatively, is there a better way to achieve what I’m attempting to? I have saved the tile as a prefab and dragged it onto the script and it does spawn 5 tiles, they just all spawn on the same spot.
public class LoadTiles : MonoBehaviour
{
// Start is called before the first frame update
public GameObject tile;
public int xMaxCoord = 5;
public int zMaxCoord = 5;
public int xCurrentCoord;
void Start()
{
for (int x = 0; x < xMaxCoord; x++)
{
xCurrentCoord = x;
transform.position = new Vector3 (xCurrentCoord, 0, 0);
Instantiate(tile, tile.transform.position, tile.transform.rotation);
}
}