- Home /
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!
I'm not sure I can help but if you post your code you're more likely to get answers. Very hard for anyone to help if they can't see what code is running.
yea that's too tricky cause its multiple long scripts... itll just confuse everyone.
All i can say for certain is that after thorough testing, if i have any sprite on screen that can be completely unrelated to sprite#1 and i destroy sprite#1, OnBecameVisible() is called for all those sprites as soon as destroy is called. It's the weirdest thing i've ever seen...
The only connection between them is the fact that all the sprites except sprite #1 are from the same sprite sheet which i don't think would affect anything.
I just don't understand how 1 sprite completely unrelated to another sprite can cause that sprite to call OnBecameVisible()... i just dont get it...
Did you know that the camera of the Scene view will call OnBecameVisible on object too? So even if your in-game camera isn't seeing the object, but the scene view still render it, you will never get a OnBecameInvisible call. $$anonymous$$aybe, that is not the issue, but it could help you find the issue.
Answer by Crazydadz · Dec 18, 2014 at 09:40 PM
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.
Hey thanks a lot for providing help :)
Yea i just had a check to make sure the function wasn't called already but I figured out a different approach to solve my problem without relying on OnBecomeVisible(), a pretty elegant one as well at least for me personally lol.
If you submit a bug report ill gladly +1 it.
Hi, I'm glad you found a different approach :).
you can vote for the issue here: Issue Tracker
Thank you :)