In this video I discuss the OnBecameVisible() and the OnBecameInvisible() callback methods that are available in Unity3D. Historically I used to write my approach to detecting when a GameObject is visible within the scene but this could easily lead to heavy CPU utilisation and race conditions. Since discovering these two methods, I’ve adapted my code and it is now a lot more efficient with certain tasks only occurring when the Renderer calls back to the GameObject to inform it that it is visible! Check out the video tutorial below.
Don’t forget, these two methods do have a slight quirkiness to them!
- Any camera that invokes the Renderer will call these methods, including the Scene view camera within the Editor!
- Even if a GameObject is slightly out of view, the Renderer can be called to provide rendering data and as such, the Renderer may call back to the OnBecameVisible() method of your GameObject leading to a false positive of sorts!
I hope that you find this Unity Tip useful! Any questions, please leave in the comments below, on YouTube or via contacting me on Twitter and I’ll endeavor to assist!