- Home /
Singleton without MonoBehaviour?
Hello, sorry for newbie question but not want make epic mistake in future.
If me need single instance class that i will use in any scene (example localization manager) i must in anyway derrive from MonoBehaviour and create GameObject for it or can just make singleton class without additional actions?
My question is actual becouse all guides tell about that need add my script to GameObject, but i cant understand why i must make it if my script can work and without Unity.
Answer by Hellium · Aug 03, 2018 at 12:36 PM
Having a "pure" C# class as a singleton is totally fine. However, keep in mind that, obviously, a "pure" C# class will not be able to receive the MonoBehaviour messages ( Awake
, Start
, Update
, OnDestroy
, ...), and won't be able to call the functions defined in this class such as StartCoroutine
, Invoke
....
Thank you. Then strange that in official guide about Localization used derrive from $$anonymous$$onoBehaviour, seems just for add support of serialized field access from inspector?
Obviously, if you want to take the advantage of the UnitySerializer, including the possibility to use the inspector to edit your data, you will need to make your class derive from $$anonymous$$onoBehaviour (to attach the script on a GameObject), or from ScriptableObject, to have an Asset in your project with the possibility to edit the data in the inspector the same way as a $$anonymous$$onoBehaviour.