OnBecameVisible seems to trigger twice or more?

Really glad if you can help me out with this issue since its quite annoying. So I made this infinite runner game. I made the groundtiles spawn whenever a groundtile become visible for the camera, however something seems to be very wrong. It’s working fine until one tile misinterprets the information and manages to start a chain reaction. Soon enough the editor has several hundreds of groundtile instances.

In detail what I’m trying to do:

When a groundtile becomes visible trigger the event “groundTileEntered”.
When the event is triggered spawn a new groundtile behind the first one.
When groundtile 1 becomes invisible destroy itself.
Repeat

Am I thinking wrong when I think this should at most keep 3 groundtiles alive at the same time?

Here’s the relevant code:

On the ground tile:

void OnBecameInvisible()
{
    Destroy(gameObject);

}

void OnBecameVisible()
{
EventManager.GroundTileEnter(transform.position);
}

On the game manager (spawner):

   void OnGroundTileEnter(Vector2 groundTilePos)
    {
        float tileWidth = 10;
        Vector2 spawnPos = new Vector2(groundTilePos.x + tileWidth, groundTilePos.y);

        GroundTileSpawner(spawnPos);
        GenerateTerrainChunk(spawnPos.x - 5);
    }

Illustration:

Add this into the code:

public float spawnTileWait = 2f; //or what ever you want the time to be in seconds
public float spawnTime; //This will be the time that the wait shall end

void OnGroundTileEnter(Vector2 groundTilePos && Time.time > spawnTime)
 {

    spawnTime = Time.time + SpawnTileWait; //This will then stop tiles from spawning and hopefully stop them from spawning after it because the cause of the multiple spawns might be because the game is detecting multiple collisions.

     float tileWidth = 10;
     Vector2 spawnPos = new Vector2(groundTilePos.x + tileWidth, groundTilePos.y);

     GroundTileSpawner(spawnPos);
     GenerateTerrainChunk(spawnPos.x - 5);
 }

Otherwise, if the box is only going to create one tile in it’s life time then you can just put this

public bool tileSpawned;

void OnGroundTileEnter(Vector2 groundTilePos && tileSpawned = false)
     {

        tileSpawned = true; //so it doesn't spawn again

         float tileWidth = 10;
         Vector2 spawnPos = new Vector2(groundTilePos.x + tileWidth, groundTilePos.y);
 
         GroundTileSpawner(spawnPos);
         GenerateTerrainChunk(spawnPos.x - 5);
     }

Just be careful with those callbacks. OnBecameVisible relates to any camera the sceneview camera in the editor included which is mentioned at the bottom of the documentation as well. I haven’t done any tests on the recent Unity version, but it’s possible that the callback will be called for each camera independently. Though it should only be called once until it’s hidden again from all cameras. Testing OnBecameVisible / OnBecameInvisible code in the editor is quite difficult You should point your sceneview camera somewhere where it can’t see your objects.

That said it’s actually a bad design to instantiate and destroy the tiles. You could simply use an object pool. instead.