Hi,
I wanna know why this function doesn’t work :
OnBecameVisible - OnBecameInvisible
public void OnBecameVisible()
{
enabled = true;
Debug.Log("VISIBLEEEE");
}
public void OnBecameInvisible()
{
enabled = false;
Debug.Log("INVISIBLEEEE");
}
I’ve 5 objects with the script which has those two function. During the execution OnBecameVisible is call only once when I see the first of the 5 objects. The function OnBecameInvisible is never called
If someone has an idea, maybe I misunderstood something !
Do you have multiple cameras in your scene? If any camera is pointing at the objects, they wont be culled.
If I recall correctly, I think in the editor, the scene camera also counts for this, so make sure your scene camera is facing away from the objects while testing too.
Indeed ! The scene camera count in the render camera :/. Now it works fine to detect function
But I want activate and desactivate their enabled variable but I can only acces to renderer.enabled, I can put it false but I can’t put it true after when they come back in the camera field.
I can’t do this on the script component too because this stop the script.
I just want detect when one/two… of the five objects are out of the cam field of view to draw a texture with the number of missing object on the screen
but when I desactivate the renderer with renderer.enabled = false when OnBecameInvisible he doesn’t reappear with the OnBecameVisible which call renderer.enabled = true;
probably as an object with deactivaed renderer is not visible and thus it doesnt get notified that it is visible again even if it would be in camera range.
what do you need this for? if it is for saving processing power i’m quite sure unity handles this itself properly.
I’m also a little confused as Unity has frustum culling by default, if the object is off screen it doesn’t get rendered anyway, so turning off the renderer doesn’t really achieve much.
Can’t you just have a boolean when it becomes invisible to show the texture?
But regardless if you want to use your approach, would it be possible to perhaps just use a material with alpha? Instead of turning off the renderer you could turn the alpha to 0 instead, so it’s still technically visible to the camera, just not to the user.
I’m not sure whether that’ll fit in with the rest of your application though, visually or performance wise.
My only purpose is to have a variable for each object to true/false.
Because in my other script when I search in all my objects with the appropriate tag those with the boolean to false I increment a variable.
The number of inc I do give me the possibility to catch the good texture in an array of 5 textures. One texture for each possible case. When an object come again in the camera field I decrement this variable.
I don’t care to hide or show the object out of the camera field I just search a variable in the Engine to do that easily but I could define my own and use it to do that.