Singleton components

As mentioned previously, static classes have difficulty interfacing with Unity-related functionality and cannot directly make use of MonoBehaviour features, such as event callbacks, coroutines, hierarchical design, and Prefabs. Also, since there's no object to select in the Inspector window, we lose the ability to inspect the data of a static class at runtime through the Inspector window, which can make debugging difficult. These are features that we may wish to make use of in our global classes.

A common solution to this problem is to implement a component that acts as a singleton—it provides static methods to grant global access, and only one instance of MonoBehaviour is ever allowed to exist at any given time.

The ...

Get Unity Game Optimization - Third Edition 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.