OnBecameVisible() called out of nowhere...

I have a 2d sprite#1 that appears on screen at which point OnBecameVisible() is called…

then i have the sprite#1 animate while calling a function inside another sprite#2,
then i destroy sprite #2 at which point sprite#1 inexplicably calls OnBecameVisible() again!!!

But it never called OnBecameInvisible()!

I have no idea why this could be happening! anyone have any idea?

It only happens as soon as i destroy the sprite#2 but its simply a damage function that has NOTHING to do with rendering sprite#1 and as i said OnBecameInvisible() is never called!

In fact after testing i noticed that even destroying sprite#2 before sprite#1 calls the damage function, OnBecameVisible() is still called! And it’s only called IF OnBecameVisible() is called initially when the sprite first appears, so if sprite#1 was off screen and i destroy sprite#2 then nothing happens.

UPDATE:
So i did more testing,
Even if i completely remove any connection between sprite#1 and sprite#2, so no references, no functions calls, NOTHING AT ALL, and i call Destroy(sprite#2) after sprite#1 called OnBecameVisible() the first time, it STILL calls OnBecameVisible() AGAIN on sprite#1… wtf???

UPDATE:
Apparently ANY sprite that exists and is visible on the screen gets OnBecameVisible() called when sprite#1 is destroyed. AMAZING!

Ok it seems to be a Unity bug, I’m getting the same issue. I’m creating 3 cubes. If I’m disabling the renderer of one of them (don’t disable the renderer of the last created one, it won’t work) this way:

if(Input.GetKeyDown(KeyCode.E))
 renderer.enabled = false;

it calls nowhere OnBecameVisible on the other cube (but only on the last created one). Not supposed to call OnBecameVisible anyway.

So you should file a bug.

A way you could avoid that issue is to double check your visibility this way

    private bool _isVisible;

    private void OnBecameVisible()
    {
        if (_isVisible)
            return;

        _isVisible = true;
        //Do what you want;
    }

    private void OnBecameInvisible()
    {
        if (!_isVisible)
            return;

        _isVisible = false;
        //Do what you want;
    }

You should create a basic project with cube to demonstrate it before filing a bug. I’ll do the same when I’ll have time.

Thank you!

David

Edit: OnBecameInvisible won’t be called if you Destroy the gameObject (I’m not sure of that). You could use the function

private void OnDestroy()
{
//Do what you want
}

to avoid this.