hey all,
im currently in the process of creating a new game and am going to be creating a manager that holds a score that refreshes every level.
moving forward i see 3 ways of doing this.
- a simple component that inherits from monobehaviour and lives in the scene on an empty gameobject
- a scriptableobject
- a static class
im wondering what is the standard approach?
personally i think scriptableobject or static class is the best approach but curious on opinons
I would advise going with a singleton monobehaviour. You can implement this easily by having a static instance of your monobehaviour that allows static access to it like so:
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
private void Awake()
{
if (Instance)
{
throw new Exception("GameManager already present in scene!");
}
Instance = this;
}
}
I’ve also seen packages that can make the whole singleton pattern a little more cleanly: GitHub - kleber-swf/Unity-Singleton-MonoBehaviour: An implementation of Singleton pattern for Unity MonoBehaviours.
Having it as a monobehaviour allows you to see and edit its properties in the inspector which is a lot nicer than having to hard code config changes to a static class. Since it’s following a singleton pattern then it’s just as accessible as a static class.
A scriptable object would be a good way to store a set configuration for a game manager but less suitable for the whole game manager class - think of scriptable object as data storage with a nice inspector interface. Since a game manager will have lots of runtime data that it’s managing and references to scene objects, it makes sense that it too should be an object that lives in scene and that it shouldn’t be trying to persist objects that only exist in scene 