Reducing the number of enabled objects by disabling objects whenever possible

Optimization principal 1: Minimize the number of active and 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 messages, such as Update()and FixedUpdate(), 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 ...

Get Unity 5.x Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.