In Unity, whats a good way to create a singleton game manager that can be accessed everywhere as a global class with static variables that will spit the same constant values to every class that pulls those values? And what would be the way to implement it in Unity? Do I have to attach it to a GameObject? Can it just be there in a folder without being in the scene visually?
Share
Like always: it depends. I use singletons of both kinds, components attached to
GameObjectand standalone classes not derived fromMonoBehaviour. IMO the overall question is how are instances bound to the lifcycle of scenes, game objects, … And not to forget sometimes it is more convenient to have a component especially referencing otherMonoBehaviourobjects is easier and safer.Startis called or have to perform actions inUpdateor other methods. Then I implement them as component and attach them to a game object that survives loading new scenes.I designed component based singletons (type 2) with two parts: a persistent
GameObjectcalledMain, which holds all components and a flat singleton (type 1) calledMainComponentManagerfor managing it. Some demo code:Now other singletons that want to register as
Maincomponent just look like: