Unloaded scenes continuously keep adding?

I’m making an endless runner, and everytime the player goes out of the camera’s view, the scene (which is called main2) restarts. However, for some reason, every time i stop playing mode, another scene-object is added to the hierarchy called “main2(not loaded)”, and i have no idea why.

I also used a boolean to make sure it’s not accidently loading the scene twice.

This is all the code im using to restart the scene:

bool hasLoaded;
private void OnBecameInvisible()
        SceneManager.LoadScene(SceneManager.GetActiveScene().name, LoadSceneMode.Single);
        hasLoaded = true;

Before anyone mentions it: yes, i also tried the scenemode “additive”, and also tried using no scenemode at all. Both gave the same result.

Hi, this system calls SceneManager.LoadScene multiple times, and this creates an error. It’s better to try and call this code only once. One way to do this would be by adding colliders around the perimeter of your camera, and then using OnCollisionEnter to pick up when the player has entered these new colliders. For more information, look for the error: “Overwriting the same path as another open scene is not allowed”.

Hope this helps. -Greg