Problems spawning tiles in ZigZag like game.

Trying to make a game like the ZigZag type game, a endless runner.

This is how the scene looks like:

And this is me method on how to spawn tiles.

public class TileManager : MonoBehaviour
{
    public GameObject leftTilePrefab;
    public GameObject currentTile;

    void Start()
    {
            SpawnTile();
    }

    void Update()
    {

    }

    public void SpawnTile()
    {
        Instantiate(leftTilePrefab, currentTile.transform.GetChild(0).transform.GetChild(0).position, Quaternion.identity);
    }
}

Which works it spawns 1 tile like this

How ever if I put the methos inside a for loop and try I assume it would spawn more tiles in a long row, how ever it just places the tiles inside each other and dosen’t form a line.

What could I possible have done wrong ?

In the loop you need to update the value of currentTile to point to the new tile you just created.

I redid the whole SpawnTile method to look like this and it now works

    public void SpawnTile()
    {
        if (_leftTiles.Count == 0 || _topTiles.Count == 0 || _rightTiles.Count == 0)
        {
            CreateTiles(10);
        }

        //random number between 0 and 2
        int randomIndex = Random.Range(0, 3);

        switch (randomIndex)
        {
            case 0:
            {
                GameObject temp = _leftTiles.Pop();
                temp.SetActive(true);
                temp.transform.position = CurrentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;
                CurrentTile = temp;
            }
                break;
            case 1:
            {
                GameObject temp = _topTiles.Pop();
                temp.SetActive(true);
                temp.transform.position = CurrentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;
                CurrentTile = temp;
            }
                break;
            case 2:
            {
                GameObject temp = _rightTiles.Pop();
                temp.SetActive(true);
                temp.transform.position = CurrentTile.transform.GetChild(0).transform.GetChild(randomIndex).position;
                CurrentTile = temp;
            }
                break;
        }

        //0 - 10
        int spawnPickup = Random.Range(0, 11);

        if (spawnPickup == 0)
        {
            CurrentTile.transform.GetChild(1).gameObject.SetActive(true);
        }

    }

Cool, glad that worked out for you.