Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question [Noob question] What's Unity's equivalent to a GameManager?

Discussion in 'Scripting' started by AjaXI, Jan 7, 2023.

  1. AjaXI

    AjaXI

    Joined:
    Sep 13, 2020
    Posts:
    9
    So I've just started learning Unity yesterday and I'm wondering if there is a better way to do what I used to do back when I worked with Unreal.
    Basically, for world related events or some gameplay elements, I used to create an empty game object, attach scripts to it and let it run the world of the game. Is there a better way to do it in Unity or is the same technique used?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    It's a good question... it leads to TONS of confusion over lifetime and uniqueness.

    Personally I favor these approaches:

    ULTRA-simple static solution to a GameManager:

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    OR for a more-complex "lives as a MonoBehaviour or ScriptableObject" solution...

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.

    OR just make a custom ScriptableObject that has the shared fields you want for the duration of many scenes, and drag references to that one ScriptableObject instance into everything that needs it. It scales up to a certain point.

    If you really insist on a barebones C# singleton, here's a highlander (there can only be one):

    https://gist.github.com/kurtdekker/b860fe6734583f8dc70eec475b1e7163

    And finally there's always just a simple "static locator" pattern you can use on MonoBehaviour-derived classes, just to give global access to them during their lifecycle.

    WARNING: this does NOT control their uniqueness.

    WARNING: this does NOT control their lifecycle.

    Code (csharp):
    1. public static MyClass Instance { get; private set; }
    2.  
    3. void OnEnable()
    4. {
    5.   Instance = this;
    6. }
    7. void OnDisable()
    8. {
    9.   Instance = null;     // keep everybody honest when we're not around
    10. }
    Anyone can get at it via
    MyClass.Instance.
    , and only while it exists.
     
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    531
    By "world-related" do you mean related to a scene or available across multiple scenes? Because it is a simple matter to create a SceneManager class and add it to the scene. This can be a subclass of MonoBehavior and does not need to be a Singleton. I use this style for every scene in my VR app.
     
  4. AjaXI

    AjaXI

    Joined:
    Sep 13, 2020
    Posts:
    9
    Thank you Kurt-Dekker and tleylan! I understand now.
     
    Kurt-Dekker likes this.