O'Reilly logo

Unity 4.x Cookbook by Matt Smith, Chico Queiroz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reducing the number of enabled objects by disabling objects whenever possible

Optimization principal 3: Minimize the number of enabled objects in a scene. Sometimes we may not want to completely remove an object, but we can identify times when a scripted component of an object can be safely disabled. If a Monobehaviour script is disabled, then Unity no longer needs to send the object Update() and OnGUI() messages for each frame.

For example, if a non-player character (NPC) should only demonstrate some behavior when the player can see that character, then we only need to be executing the behavior logic when the NPC is visible—the rest of the time we can safely disable the scripted component.

Unity provides the very useful events OnBecameInvisible() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required