Ending a loop

I am spawning tiles for an infinite runner game but at a certain distance they need to stop spawning, what are some options for how to get the tiles to stop spawning?

using UnityEngine;
using System.Collections;

public class TileManager : MonoBehaviour {

    public GameObject[] tilePrefabs;
    private Transform playerTransform;
    private float spawnZ = 0.0f;
    private float TileLength = 24.62f;
    private int amnTilesOnScreen = 10;
    // Use this for initialization
    private void Start () {
        playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;

        for (int i = 0; i < amnTilesOnScreen; i++) {
            SpawnTile ();
        }
    }
   
    // Update is called once per frame
    private void Update () {
        if (playerTransform.position.z > (spawnZ - amnTilesOnScreen * TileLength)) {
            SpawnTile ();
        }
   
    }
    private void SpawnTile(int prefabIndex = -1)
    {
        GameObject go;
        go = Instantiate (tilePrefabs [0]) as GameObject;
        go.transform.SetParent (transform);
        go.transform.position = Vector3.forward * spawnZ;
        spawnZ += TileLength;
    }

}

You can disable TileManager on some condition met.

Or in TileManager have a bool that you set for ‘reached end’, and Update TileManager stops spawning tiles if that bool is set, and of course toggle that bool on some condition met.

What matters is… what condition do we want to disable upon. You need a way to monitor for that.

Make a public bool isSpawning. Set it to true at the start. Check it in Update before you call SpawnTile.