How do i stop OnBecameInvisible being used when I exit game?

Noob question, but I’ve been googling for the past 2 hours with no avail.

Im getting rid of objects by using OnBecameInvisible, and using this to call on a function that will replace the objects that have been deleted to the front of the stack.

When I exit the game, I get a bunch of created objects as if the player ran past them with this error

Some objects were not cleaned up when closing the scene. (Did you spawn new GameObjects from OnDestroy?)

I tracked the issue down to the OnBecameInvisible function… how can would I stop this being ran when I exit the game so that I don’t get a bunch of random clones in my editor scene.

this is a endless runner type of game, I don’t think It would effect the game but running the game to test it constantly creates these objects I have to delete manually.

Should I just stop using the OnBecameInvisible function all together? and go to a system that uses colliders with OnCollisionExit? or am I able to remedy this situation somehow?

Thanks in advance fellas.


public class Platform : MonoBehaviour
    private InfinitePlanes infinitePlanes;

    private void OnEnable()
        infinitePlanes = GameObject.FindObjectOfType<InfinitePlanes>();

    public void OnBecameInvisible()

TLDR the RecyclePlatform creates more platforms at the front of the terrain stack.
once I exit game, it creates a bunch of clones I have to delete at the front of the stack.
how to stop it from doing its job when I quit the game? as that’s going above and beyond its needs.

I feel like once I post a question on the forums, I go into overdrive to figure out the problem I’m having.

this example from a completely different situation has helped me solve this issue.

bool quitting;

 void OnApplicationQuit() {
     quitting = true;

 void OnDestroy() {
     if (quitting) {
         //application is quitting; don't spawn more stuff
     } else {
         //application is running, proceed normally