Disabling objects by visibility
Sometimes, we may want components or GameObjects to be disabled when they're not visible. Unity comes with built-in rendering features to avoid rendering objects that are not visible to the player's camera view (through a technique known as Frustum Culling, which is an automatic process) and to avoid rendering objects that are hidden behind other objects (Occlusion Culling, which will be discussed in Chapter 6, Dynamic Graphics), but these are only rendering optimizations. Frustum and Occlusion Culling do not affect components that perform tasks on the CPU, such as AI scripts, user interface, and gameplay logic. We must control their behavior ourselves.
A good solution to this problem is using the OnBecameVisible() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access