Spawn on trigger

So, I am creating an endless runner game and I should spawn some tiles. I decided to use the trigger enter method.This code snippet goes into the each individual platform script,so I instantiate.

   private void OnTriggerEnter(Collider other)
        {
            if(other.tag == "Player")
            {
                         
                FindObjectOfType<TileManager>().shouldChange = true;            
                StartCoroutine(WaitAndChange());
            }
        }
IEnumerator WaitAndChange()
    {
        yield return new WaitForSeconds(waitFor);
        FindObjectOfType<TileManager>().DeleteTile();
    }

This code snippet goes into the tile manager. Tiles move towards player not vice versa, so illusion of movement is created and everything is optimized

private void LateUpdate()
    {
        if(gameStartScript.gameStarted && playerController.isAirborn == false)
        {
            //if (gameStartScript.GetFirstTile().GetComponent<RoadStart>().shouldMove == true)
            //{
                gameStartScript.GetFirstTile().transform.Translate(gameStartScript.GetFirstTile().transform.forward * playerController.speed * Time.deltaTime);
            //}
            foreach (var tile in activeTiles)
            {
                tile.transform.Translate(tile.transform.forward * playerController.speed * Time.deltaTime);
            }
        }
    }

This code snippet is connected to platform, so when I enter the platform’s trigger there is a switch and I tile is created

if (shouldChange)
        {            
            SpawnTile();
            shouldChange = false;//so i create tile once
            //SpawnTile();           
        }

Tile creation function: I instantiate a random tile from list and attach tile at the end of the last instantiated tile. It works fine if speed is not too high

GameObject tile = Instantiate(prefabs[Random.Range(0, prefabs.Count)], new Vector3(activeTiles[activeTiles.Count - 1].transform.position.x + xAdditive, gameStartScript.GetFirstTile().transform.position.y, activeTiles[activeTiles.Count - 1].transform.position.z + tileLength), gameStartScript.GetFirstTile().transform.rotation);           

You might wonder: what is the problem?So when I set the player’s speed or some time passes (which is actually how fast
ground is scrolled) a bit high then tiles don’t instantiate. They don’t, and player just falls off the grid. What is wrong with these scripts?

I pretty much liked this video. Hopefully it helps you too.