Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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
0
Question by runningTurtle · Jul 24, 2015 at 06:33 AM · c#singleton

Shouldn't Singletons not extend Monobehaviour ?

Hello, almost all examples of Singletons I've seen so far have the singleton class extend MonoBehaviour which seems strange to me.

I'm pretty new to Unity and C# so I may be (probably) wrong, but It seems to me that besides the fact that extending MonoBehaviour makes it easier to "grab" stuff in the scene, this approach only has downsides, for example:

  • you need to create a game object and then attach the singleton,

  • you need to use DontDestroyOnLoad to make it persist between scenes

So I guess my question would be: when creating singletons, is it not better to have them NOT extend MonoBehaviour ? In this case there's no need to attach them to any GameObject and they are available everywhere and all the time ?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Veldars · Jul 24, 2015 at 07:00 AM

Hi, it's a good question, I will follow other answer ^^.

For me that depend of your singleton usage. A singleton in unity could be by example a chat window that you need on all your game scene so it need to extend of monobehaviour. But if you need no function from MonoBehaviour(and no render) I think yes you don't need to extend your singleton...

But it's a general rules, if your class doesn't need any function from MonoBehaviour why create this class as a subclass of monobehaviours...

Comment
Add comment · Show 1 · 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 Bunny83 · Jul 24, 2015 at 07:31 AM 0
Share

exactly ^^ +1

avatar image
1

Answer by vexe · Jul 24, 2015 at 07:37 AM

No you don't need to inherit MB. There's more than one way to achieve a singleton like behavior, depends on what you're doing, and your style. I'll share the singles I'm using

 using UnityEngine;
 
 public static class SingleScript<T> where T : ScriptableObject
 {
     private static T _instance;
     public static T instance
     {
         get
         {
             if (_instance == null)
             {
                 _instance = Object.FindObjectOfType<T>();
                 if (_instance == null)
                 {
                     var path = "Singles/" + typeof(T).Name;
                     _instance = Resources.Load<T>(path);
                     if (_instance == null)
                     {
                         if (typeof(ScriptableObject).IsAssignableFrom(typeof(T)))
                         {
                             _instance = ScriptableObject.CreateInstance<T>();
                             #if UNITY_EDITOR
                             UnityEditor.AssetDatabase.CreateAsset(_instance, path);
                             #endif
                         }
                     }
                 }
             }
             return _instance;
         }
     }
 }
 
 public static class SingleBehaviour<T> where T : MonoBehaviour
 {
     private static T _instance;
 
     public static T get()
     {
         return get(true);
     }
 
     public static T get(bool create)
     {
         if (_instance == null)
         {
             _instance = Object.FindObjectOfType<T>();
             if (_instance == null)
             {
                 if (create)
                     _instance = new GameObject(typeof(T).Name).AddComponent<T>();
                 else
                     Debug.Log("Couldn't find object of type: " + typeof(T).Name);
             }
         }
         return _instance;
     }
 }

Then I have a globals class:

 public static class globals
 {
     public static TextUI text { get { return SingleBehaviour<TextUI>.get(false); } }
     public static Database database { get { return SingleScript<Database>.instance; } }
     public static Tasks tasks { get { return SingleBehaviour<Tasks>.get(); } }
 }

Comment
Add comment · 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
1

Answer by ScienceIsAwesome · Jul 24, 2015 at 12:13 PM

Another reason to extend MonoBehaviour is easy access to serialized fields in the inspector (variables of the script visible and changeable in the editor). Of course you can create an Editor Window for that:

 using UnityEngine;
 using UnityEditor;
 
 class GameManagerWindow : EditorWindow
 {
     [MenuItem ("Window/Game Manager")]
     
     public static void  ShowWindow ()
     {
         EditorWindow.GetWindow(typeof(GameManagerWindow));
     }
     
     void OnGUI ()
     {
         GameManager.something = GUILayout.HorizontalSlider(GameManager.something, 1, 5);
         GUILayout.Label ("Current value of something is " + GameManager.something);
     }    
 }

Note that this script has to be in the Assets\Editor folder, you can create it if it doesn't exist. And then there's the Singleton:

 using UnityEngine;
 
 public class GameManager
 {
     public static float something;
 
     protected GameManager() {}
 
     private static GameManager _instance = null;
     
     public static GameManager Instance
     {
         get
         {
             return GameManager._instance == null ? new GameManager() : GameManager._instance;
         }
     }
 }


GameManager.something can now be read and even changed from anywhere, manipulated in the custom Editor Window and it survives scene loading, it even survives dropping out of play mode :)

If you want it to survive going into play mode, there's a simple trick with a MonoBehaviour:

 using UnityEngine;
 using System.Collections;
 
 [ExecuteInEditMode]
 public class NewBehaviourScript : MonoBehaviour
 {
     public float something;
 
     void OnApplicationQuit ()
     {
         PlayerPrefs.SetFloat("something", something);
     }
 
     void Awake ()
     {
         something = PlayerPrefs.GetFloat("something");
     }
 }

This "something" variable is never reset to anything, you change it in play mode or edit, this can be done with a Singleton : MonoBehaviour if you want really persistent variables...

Comment
Add comment · 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
0

Answer by kemar · Jul 24, 2015 at 07:04 AM

Hi, If you don't need Coroutines, update, onClick... or other function of MonoBehaviour don't extends MonoBehaviour to your singleton this is useless.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity C# Singleton? 6 Answers

What threads are used in a scene? 1 Answer

Access Inventory from player 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