Instantiate bug

Making a 2d side scroller car game where the ground is made out of chunks of terrain(length of camera). When a chunk leaves the screen entirely, it will destroy itself. When it is fully present in the camera, a new chunk is spawned right next to it.

However, it loads only one chunk in my entire playthrough.
Any ideas?

public class worldGen : MonoBehaviour
{
    void Update()
    {
        var thisLand = this.gameObject; // original chunk

        transform.Translate(-Vector3.right * 4 * Time.deltaTime, Space.World); // moves towards the left
        
        if(transform.position.x + 42.75f < Camera.main.transform.position.x) // if out of sight
        {
            Destroy(this.gameObject);
        }

        

        if(thisLand.transform.position.x == Camera.main.transform.position.x - 0.16f) // if fully within the camera
        {
            Debug.Log("intantiated");
            
            var newLand = Instantiate(this.gameObject); // instantiate new chunk
            newLand.transform.position = thisLand.transform.position + new Vector3(42.75f, 0, 0);// setting the position of the chunk
            
            Debug.Log(newLand.transform.position.x);
        }


    }
}

While constantly adding or decreasing a floating point number, it’s very unlikely that you’ll ever end up at an exact value. Checking transform.position.x == something is very likely to never yield true.

When you fix that, remember to make sure you’re not spawning a new chunk in each and every frame :wink:

I don’t know how your scene is set up, but I’d guess that you might also destroying the object with this component on before instantiating a copy - under certain conditions.