• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by alexanderflink · Jan 21, 2016 at 08:21 AM · c#inheritancemonobehaviourclassesderived-class

Making a static class derive from MonoBehaviour in C#

Hello!

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:

 GameManager.SetScore();
 SoundManager.PlaySound();

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. Thanks!

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Jan 21, 2016 at 10:08 AM 0
Share

It's more complicated to code than the static approach, but I often make these Singleton classes instead. Depending on what they're doing, they might have a Scene lifetime or exist for the whole lifetime of the Application (in the the latter case I'd usually put them in a loading or bootstrap scene ie one which is only entered once, on starting the application).

An advantage of this approach over the static way of doing things is that it enables you to give them public variables that you can set in the inspector, making it easy to tweak things.

avatar image alexanderflink Bonfire-Boy · Jan 21, 2016 at 10:15 AM 0
Share

Yes, I tried making a Singleton class like that. Like the one they make in the Unity 2D Roguelike tutorial, if you've watched that. But I found that a major flaw with that approach is that you have to load the bootstrap scene in order for the game to work. This is pretty annoying when testing levels out etc...

avatar image Bonfire-Boy alexanderflink · Jan 21, 2016 at 10:30 AM 0
Share

There are several possible ways around that.

Often it means that that particular manager doesn't need to have an application lifetime (like I said, with some you want to make a new one for each scene).

Another approach is to use lazy application-lifetime singletons, ie they're created when first required (you can still get the advantage I mentioned in my final para above, by creating them from prefabs).

Or I might use both the bootstrap method and the lazy one. For example with a class managing localisation I might have an instance in my bootstrap scene which checks the device's locale and loads the strings for the right language. But when there isn't a localisation manager (including when I've bypassed the bootstrap scene while developing in the editor), a simple lightweight one is created on the fly which doesn't need to bother with any actual localisation.

Like I said, the Singleton approach is more complicated. But it's also more powerful and flexible.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by hulahoolgames · Jan 21, 2016 at 08:49 AM

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.

Comment
rob5300

People who like this

1 Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexanderflink · Jan 21, 2016 at 08:58 AM 0
Share

So I could have like a ManagerController or something, that's instantiated on the scene, and just calls functions on the managers on update / awake etc? That sounds like a pretty good idea! Thanks. :)

I'll keep this open to see if anyone else has another suggestion.

avatar image rob5300 · Jan 21, 2016 at 10:22 AM 1
Share

This would be the best way to approach this. For reference you may want to use a Static Constructor aswell, assumuming you are using c#. Take a look here for the documentation: https://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx

avatar image KazYamof · Jan 21, 2016 at 11:58 AM 0
Share

Why dont you create only the methods you need static as static and let the class untouched?

avatar image alexanderflink KazYamof · Jan 21, 2016 at 01:00 PM 0
Share

Well, I'd still need to instantiate the class as a script on a gameobject then, right? I would prefer not to have to instantiate all of the manager classes i'll have, since it's unnecessary.

avatar image rob5300 alexanderflink · Jan 21, 2016 at 03:20 PM 0
Share

Can you give us some background do what your GameManager and SoundManager do? As for example if they need to use the MonoBehaviour events frequently maybe having a MonoBehaviour with some static methods could be a good idea as long as you are comfortable with static members mixing with non static members.

You could try having a single MonoBehaviour called Events to simply manage calling certain static methods from your managers from Update, ensuring that only one MonoBehaviour exists and you can keep the managers as purely static classes.

Show more comments
avatar image

Answer by rob5300 · Jan 21, 2016 at 03:25 PM

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: https://msdn.microsoft.com/en-us/library/ak9w5846.aspx

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by tomachinz · Sep 07, 2021 at 02:31 AM

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Is it possible to change value on base class when the same field gets changed on it's derived class 0 Answers

Distribute terrain in zones 3 Answers

How do I call function in inhereted script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges