I would like to ask some nuanced questions about Game Managers specifically within Unity. I have a formal Computer Science degree (but 20 years ago) and have written SW in Java (also 20 years ago). Minimal C# experience. I am familiar with CS theory, thread safety, and basic familiarity with design patterns like static classes, singletons and service locators.
In short - I don’t want to debate design patterns generically but rather I want to understand what’s different about C# and Unity.
I am new to Unity and C# and so I don’t know what to do with all the variance in everyone’s approaches to game managers. (Singletons seemed easy in Java). With my questions below, I am trying to develop a nuanced understanding of why the advice is so divergent.
More context: The game I am trying to write will have multiple scenes. The game will need save/load capability. At some point it will be multiplayer.
-
Should my game managers extend from MonoBehaviour? What are the reasons for or against?
-
RELATED: What are reasons for or against having the game manager attached to a scene hierarchy object?
-
I don’t understand why some sample code for game managers uses generics. What problems do generics solve specific to a game manager?
-
I thought I read that you can’t define your own constructors if you extend from MonoBehavior, yet I see sample code with constructors defined and even these constructors being called. What am I misunderstanding?
-
It seems that DontDestroyOnLoad is necessary to preserve the singleton across multiple scenes but I saw a comment on this thread arguing against it. Is the person basically saying that if you don’t extend MonoBehaviour (which ties to question 1) you don’t need to worry about DontDestroyOnLoad? Reference: [Help] How do you set up a GameManager?
-
I’ve seen some code destroying GameObjects that already exist to preserve the singleton-ness. Sometimes it is just destroying “this”. Sometimes the code even keeps track of a list of instances to destroy. Surprised to see all this exception handling. Is this an issue only if you extend from MonoBehaviour? Reference: c# - In Unity, how do I correctly implement the singleton pattern? - Game Development Stack Exchange
-
Surprised to see so little code and discussion on protecting against serialization, cloning, reflection. What am I missing?