Why OnBecameInvisible is called twice times when the object is off the screen?

Hi all

I’d like to fire an event only then my gameObject is out of screen and so I use OnBecameInvisible() function (I don’t want to use Renderer.isVisible).

Everything works expect that OnBecameInvisible() is called two times when the object is off screen and not only once.

Do you know why this behaviour?

P.s. I have neither duplicate gameObject nor duplicate function.

Thank you for help?

If you’re getting two OnBecameInvisible’s without any OnBecameVisible between them, the solution is to record if you’ve done the onBecameInvisible already:

bool isVisible = true;

void OnBecameInvisible() {
    if(isVisible) {
         //Do yr stuff
    }
    isVisible = false;
}

void OnBecameVisible() {
    isVisible = true;
}

This assumes that there’s an actual buggy behaviour. If what’s actually happening is that the object rapidly becomes invisible, visible and then invisible again, you’ll have to do some kind of time-based check:

float invisibleTime;

void OnBecameInvisible() {
    if(Time.time - invisibleTime > .5f) {
        invisibleTime = Time.time;
        //Do yr stuff
    }
}

Hope that helps.