0
\$\begingroup\$

I did notice that if I assign scripts that generate singleton classes (for my managers), to an empty game object, everything works fine.

Now I would like to start the scene with the empty game object, attach the main game manager singleton, and in the game manager singleton I would like to attach to it the other manager singleton classes.

Although I am not sure how you actually do so. Do you run addComponent to the main game manager, and they will be attached to the existing gameobject?

The gameobject that I use for the main gamemanager is set to not be destroyed on load; the doubt is if I should run addComponent on the scripts, or create a reference from the gamemanager script and instantiate it from there.

I did look for examples on the subject, but the only thing that I found is the reference to add a script to a game object; and I am not sure if that's actually what is supposed to be done.

\$\endgroup\$
1
\$\begingroup\$

A MonoBehaviour is always added to a GameObject with AddComponent. Unfortunately AddComponent will always instantiate a new object, which means that a MonoBehaviour can not be a singleton.

However, MonoBehaviours can use singletons. When you would like to add a singleton-component to multiple game objects, create a separate MonoBehaviour which is instantiated but then calls the singleton internally for all of its methods.

When your singleton looks like this:

public class MySingleton { 
     private static MySingleton _instance;
     public MySingleton Instance {
        get { 
             if (_instance == null) _instance = new MySingleton();
             return _instance;
        }
     }
     /* methods */
}

Your proxy can work like this:

public class MyBehaviour: MonoBehaviour {

      void Update() {
           mySingleton.Instance.Update();
      }
}

A simpler method to share data between instances of the same MonoBehaviour is to just declare those variables as static.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can do it simply by using DontDestroyOnLoad

  • Add your manager Game Object in splash screen.
  • Attach all the scripts you want to it.
  • In any one script call DontDestroyOnLoad in Awake.

void Awake(){ DontDestroyOnLoad(gameObject); }

This game object will last through out the game.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yes, I am aware of that; the problem is not the fact that the object is destroyed, since I did already use DontDestroyOnLoad; but what is the correct way to add other singleton to that gameobject. I can add them via UI in the editor, but I would rather rely on structured code, to add the other managers. And was wondering if "addcomponent" was the correct way to do so. \$\endgroup\$ – rataplan Jan 31 '16 at 10:41
  • 1
    \$\begingroup\$ I see, I think AddComponent is not that best way. What you can do is to make prefabs for every manager with scripts attached and instantiate them through main manager which is alive in the scene from the beginning. And your main manager and all prefabs must have DontDestroyOnLoad block \$\endgroup\$ – Hamza Hasan Jan 31 '16 at 10:48
  • \$\begingroup\$ I see, so you suggest to have multiple gameobjects saved as prefab, and instantiate them at runtime when the gamemanager is active. Which is the benefit of this approach, versus having all the scripts connected to a single gameobject entity? \$\endgroup\$ – rataplan Jan 31 '16 at 11:06
  • \$\begingroup\$ Clean n clear, easy to handle,it does instantiate only that elements what you want to use. Otherwise all the elements will be would execute their initial code with and without need. For example, I need an Ad Manager, Inapp Purchase Manager and Sound Manager then I'd never do it in that way. I'd keep them separate, as they should be. \$\endgroup\$ – Hamza Hasan Jan 31 '16 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.