Manager systems for Unity3d, a new approach (OBSOLETE)

UPDATE: This is not something I would recommend anymore and I intend to write on the subject soon.

As I said on a previous post, it is very common for Unity3d developers to come up with the idea of a game object in their scenes that will hold components that will act as singletons, right?

Normally we want this game object to be there from the beginning, make some initialization (such as finding entities in the scene) and then be ready to handle our requests. Also, we would like this game object to be persistent so we would like to call DontDestoyOnLoad(this); on it. This approach works very well if you have a single scene in your project as everything is running on the same context, but if you have multiple scenes, say, multiple levels in your game, things can get complicated. Suppose you have a component attached which purpose is to find the most used entities in your game (such as the player) and cache them, allowing other components to access them with a static variable. The problem with multiple scenes arises if you change scene and don’t update the references to your cached game objects.

I’ve been using a slightly different approach that threats the whole system as a FSM (Finite State Machine). Let me exaplain:

The first idea is to create a static class that will hold a reference to any manager we have in the game, for example:

As you can see, we have a bunch of things that will let us access those Managers easily, as easy as:

Obviously, those components (like the AudioManager) must be attached to this game object in order for this to work. The idea behind this managers is that they will be “always ready”, like a system service, and that they don’t have any dependencies with any particular scene. For example, the AudioManager is able to play sounds in any scene and situation.

There are other types of managers, those that rely on a particular scene or state in order to work. There is also the problem of the global game state. Introducing your awesome GameManager class:

And it’s loyal companion, the GameState abstract class:

Those classes make up a simple FSM system for the different game states in your game (paused, playing, menu screen, etc.). You should inherit the GameState class to provide your custom states. A state is also a component attached to a game object that is responsible for activating/deactivating each game manager that is dependent on that particular state. For organization, I suggest you attach your states to game objects with the name of the state that are childs of the manager game object, like this:

Manager object distribution

Manager object distribution

This way, you can have sub-managers attached to this child objects and everything is perfectly organized.

Hope you find this useful. I will post more information about this soon.

  • Garry Williams

    I'm really fond of creating a lot Manager class when building a game, it's a very good way of maintaining a clean interface for every subsytems while allowing a lot of interaction between them.

    This is definitly an interesting way to improve from the static instance implementation :)

  • alexfeature

    Hey Mate,

    I've been playing with a few different implementations of a similar system but your way is less complex and is much easier to maintain and extend.

    Thanks a lot for sharing.

  • http://headkit.myopenid.com/ hk

    looks very interesing but a bit too complicated for noobies…
    if you have some examples we would be very pleased!

  • http://headkit.myopenid.com/ hk

    so how do I exactly use this?

  • Kevin

    Awesome! We are using a similar system right now! Keep pushing the limits and expanding ideas man! Good to see that you are innovating! :)

  • http://headkit.myopenid.com/ hk

    could you please post a simple and short example of usage?
    thanx – great work!

  • http://www.silentkraken.com/ Seth Illgard

    Will do, very soon. Currently I'm super busy as I am preparing to move the United States, but expect updates soon.

  • http://www.silentkraken.com/ Seth Illgard

    Yes, as soon as I have time I'll add some examples. Sorry for the delay.

  • http://headkit.myopenid.com/ hk

    cool – that would be great!
    thank you

  • http://headkit.myopenid.com/ hk

    I allways get the error “NullReferenceException: Object reference not set to an instance of an object” when I call my Managers.Background.SetNewTexture()-Method…
    :-(

  • D3d

    I'm looking forward to more info on how to use and implement the (C#)game manager you've shared here. I'm getting it, but could really use some of your great descriptive posts. Have a good move

  • Bla

    *…*

  • Hooligan_

    Just trying this out and the line in the Awake method look funky.

    gameManager = GetComponent<GameManager>();

    Are they supposed to be like that? Because my compiler doesn't like it very much.

  • Hooligan_

    Im guess its supposed to be

    gameManager = GetComponent(GameManager());

    PS: Thanks for the little tut. Its very helpful.

  • http://www.silentkraken.com/ Seth Illgard

    You should be able to do:

    Managers.Audio.Play(transform.position, clip);

    or something like that. They are static fields :)

  • Millerpaez

    please post soon some basic examples. excellent work

  • mipago

    thanks for the code. would be good if all your posts were illustrated with examples

  • Guest

    Dude, it would be awesome if you posted your example :)

  • Adam

    I'm attempting to incorporate this system in a simple test. I have the managers class, the GameManager and the GameState class. I have inherited two GameState classes, a MenuState and a PlayState. In the GameManager Start() function i have:

    SetState(typeof(MenuState));

    now I'm wondering how to link these to scenes in Unity. So in MenuState's OnActivate() I call:

    Application.LoadLevel(“Menu”);

    then in OnUpdate() i have:

    timer += Time.deltaTime;

    if(timer >= 120)

    {

    ManagerManager.m_game.SetState(typeof(PlayState));

    }

    so in PlayState OnActivate() I call:

    Application.LoadLevel(“Game”);

    However I never get this far. In Unity do I have to set up GameObjects that just contain the GameState scripts? I did this and nothing happens. So I tried making them children of the GameManager. The problem with that is when the Play scene is loaded the GameManager object no longer exists. But I never get that far anyway. For some reason multiple GameManager objects are spawned at run time, it looks like almost every update creates one, then the GameManagers Start() function keeps getting called. I think something like an infinite recursive loop is happening here, but I have no idea why.

    Can anyone offer any assistance?

  • Nick

    Hi,

    i just implemented this approach and want to let you know how i did it to get it working in case you have problems, so just a quick guide:

    If you have the classes as said above, you want to implement a few classes extending the GameState abstract class, for example: StateGamePlay, StateMainMenu, StateIntro and so on.
    On the entry scene of the game i place an empty gameobject calles Managers (you can call it what you want, e.g. “TheGame”).
    In this empty gameobject you drag the Managers Class (the one from above with all the managers in it), also all the Managers you created, and also the state classes -> in order to be found by the method GetComponentInChildren. These are the ones extending GameState, the are not of MonoBehaviour but you can drag them on the GameObject.
    Actually then everything should be fine. You can switch the States.

    Cheers,
    Nick

  • Philip Atha

    I realize this is over a year old, but did you ever figure this out? I’m getting the same error when trying to call SetState from each sub GameState object.

  • Marie Weaver

    Using the FSM system components, you develop the multiple
    levels in your game and audio manager is able to play sounds.

  • http://www.facebook.com/nerophon Nicholas Bellerophon

    I use an almost identical system for my Unity 3d development, though some of the names are different. Great minds think alike? Actually I think your names may be slightly better than mine. However, my state machine is more powerful: I’ve created a transition system for moving between states based on an abstract “ModeTransition” class, for those cases where you want a particular transition to do things that require knowing both the From and the To.

  • Giddeon

    Hey Daniel.
    I really like your Manager System, but im a totally beginner on Unity and C#.
    Is it possible that you give us a more explained tutorial ?
    At the moment, im confused how to setup Unity and scenes to get it work.
    Whats the System.type var MenuState ? How my Managers work on new scenes (Menu, Intro, GameLevels) do i add these ManagerScripts to all my scenes ?
    Questions over questions (and a bad english XD )…
    Maybe you could give us a hand ^^

  • Ray

    I’ve been using this approach, too, and I like it. I know it’s been 3 years but could you elaborate why you wouldn’t recommend it anymore and present your alternative? I can’t find your post on it anywhere. If you’re still alive, I’d appreciate more information – I always look for better techniques to make my life easier :)

  • Phil Winkel

    “UPDATE: This is not something I would recommend anymore and I intend to write on the subject soon.”

  • https://smartbaba.ae/ smartbabaae

    Thats nice post… Lets start Unity 3D Game Development from Basic Free http://smartbabaae.blogspot.com/2016/10/how-to-start-unity-3d-game-development-guide.html