How do I use OnBecameVisible and OnBecameInvisible?

Hi all,

Unity's optimization page suggests using the OnBecameVisible/OnBecameInvisible functions.

I have a cube that is outside the camera's view frustum (its about 30 meters behind the player) and I have a script attached to the cube with the following code:

function OnBecameVisible () {

print("VISIBLE");

}

function OnBecameInvisible () {

print("INVISIBLE!");

}

I'd like to have it print a message when the cube is in the camera's frustum and when it leaves the camera's frustum. Unity's documentation reads, "This message is sent to all scripts attached to the renderer." The cube does have a mesh renderer and when the scene starts I receive a "VISIBLE" (even though I start not facing the cube) and then when I face the cube and turn away (180' or an about face) it doesn't give me the "INVISIBLE" message. As I said I'm ~30 meters away so I'm confused why this is happening. Also it will only give me a "VISIBLE" message on start up. Looking away and back at the cube produces no more messages. Any suggestions? Thanks!

Make sure the editor/scene camera isn't pointing at the cube. It counts for rendering it too

Otherwise, your code looks fine

Another gotcha: it seems dynamic shadows also activate "visibility" (presumably because they are projected). So you would need to turn off "Cast Shadows" on the cube too.

hi i have exact same problem but none of the trick you friend said not work it always print the message

i dont understand that you say camera pointing the object

but if you mean that camera not show the object in editor at first i do it and its not work

i even disable cast and recieve shadow please help me