Making a static class derive from MonoBehaviour in C#


I’m just learning about “more” objective oriented programming in C#. I already made a small mobile game in UnityScript, but that turned out as spaghetti code.

Is it possible for a static class to inherit from MonoBehaviour? Because i’m making some classes like GameManager, SoundManager, UIManager etc. I want to be able to call functions on these from any GameObject, like so:


Without having to have a reference to the specific manager. Hence, I want them to be static. However, i’ll need to use the built-in functions like Update, Awake etc in these managers. It seems I can only do this if the class derives from MonoBehaviour, and it can’t if it’s static. It just throws an error.

Am I thinking backwards here? If so, please tell me how you would do these managers.

Well one thing you can do is, have static classes for these and call the required functions from the Monobehavior funtions of other classes. For example, say you have a SplashScreen UI that has a Monobehavior. In its Awake() you can call GameManager.init(), SoundManager.init() and do the initialization of the managers. Also instead of having Gamemanager and SoundManager their own updates, call the appropriate functions in the manager classes from the updates of other monobehaviors. Hope this helps.

Copied from my comment:

“You could try having a single
MonoBehaviour called Events to simply
manage calling certain static methods
from your managers using Update() or Start() or FixedUpdate() or even OnLevelWasLoaded(),
ensuring that only one MonoBehaviour
exists and you can keep the managers
as purely static classes.”

You could even go into using Interface events. I haven’t tried this myself but try reading the MSDN page about it here: How to implement interface events - C# Programming Guide - C# | Microsoft Learn

Yeah you can do that, it’s called a Singleton. But the inspector would need to built as it could never be selected nor attached to a game object unless you want to mix instances with singletons. For my project I am putting those into an EditorWindow which can always stay visible, and made to appear from the Window menu. It seems there is multiple ways to do things, which is nice.