- Home /
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!
Answer by Mike 3 · Jul 26, 2010 at 08:43 PM
Make sure the editor/scene camera isn't pointing at the cube. It counts for rendering it too
Otherwise, your code looks fine
i have another problem i want when the object is visible move that by transform.Translate() but because it is not in update method it just move a bit and then not moving
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
$$anonymous$$aybe you have attached your script on to object without any renderer...
Im having trouble with $$anonymous$$e. It can detect the object even though its behind walls and whatknots. I cant use raycast since the rooms I use are technically one object with flipped faces.
Answer by Waz · Aug 21, 2010 at 05:19 AM
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.
Your answer
Follow this Question
Related Questions
How to reinstate a disabled object when becomes visible? 1 Answer
OnBecameVisible and OnBecameInvisible with LOD group. 2 Answers
Activate Game object when in Camera view and deactivate when out of camera view. 2 Answers
why the function OnBecameVisible and OnBecameInvisible doesn't work in canvas renderer ? 1 Answer
How do I stop a moving object when the Camera sees it 1 Answer