- Home /
gamemanager: monobehaviour, scriptable objects, or static class
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. 1. a simple component that inherits from monobehaviour and lives in the scene on an empty gameobject 2. a scriptableobject 3. 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
Answer by jackmw94 · Jan 15, 2021 at 09:40 AM
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: https://github.com/kleber-swf/Unity-Singleton-MonoBehaviour
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 :)
thank you so much! Learning the balance between unity scripting and traditional c# has been an educational challenge so i appreciate your deep dive :)
Your answer
Follow this Question
Related Questions
How to save score for survival time? 1 Answer
Serialization of an object in unity causing IO Exception Sharing Violation At Path 1 Answer
Add point on click of cube? 1 Answer
How to create a HighScore using playerprefs based on passed time? 1 Answer
Ive been trying to get a score counter to work for 2 weeks please help. 0 Answers