Singleton & Attached GameObject with scripts

Hello,

I’ve got some problem with singleton.
I have a GameManager with a script GameManager.cs which is a Singleton.
Under the GameManager Object, I have many other GameObjects with attached scripts.
All this structure is a prefab.

When I load a new level, the GameManager prefab singleton is working perfectly (same instance ID … still running some processes …). But it seems that children’s scripts are initialized, and restart with Awake(), Start() and OnEnable().

Is there a way to avoid this ?

Thanks four your help

How are you loading a new level? Application.LoadLevel or SceneManager in Unity 5.3? Pushing the play button in the editor?

If you’re changing scenes and your child objects’ scripts getting their Awake/Start calls again, then I’d suggest that your Singleton is being destroyed during the scene load and re-created immediately. It could appear to be the same instance of the singleton because it is created from a prefab, and may make use of static variables instead of instance variables. Be certain to call DontDestroyOnLoad( singleton.gameObject ) somewhere before you unload the current scene.

If you are certain that the gameobject of your singleton is marked with DontDestroyOnLoad, and you are certain that you are not somehow manually destroying and recreating your child objects each scene, then you may have found a bug. When a game object is marked DontDestroyOnLoad, its children also receive that benefit as well.

The child objects should not be re-initialized if the parent object is a DontDestroyOnLoad object.

Is the GameManager singleton in every scene, and then deleted if it already exists? Ie. does your code look somewhat like this:

public class GameManager : MonoBehaviour {

    public static GameManager singleton;

    void Start() {
        if(singleton != null) {
            Destroy(gameObject);
        }
        else {
            singleton = this;
            DontDestroyOnLoad(gameObject);
       }
    }
}

In that case, the child objects will have time to call their Awake/Start/OnEnable before the destruction. The methods are not getting called on the old child objects from the old scene, but on the ones in the new one that’s going to be destroyed shortly.

The easiest fix is to not have the GameManager singleton placed in every scene. Instead, place an object that instantiates the GameManager prefab if the singleton doesn’t exist.

kru >

I’m on 5.3.1. I’m using, SceneManager.LoadLevel, but it’s the same thing with the old Application.LoadLevel.
Singleton definition is right :slight_smile: (Baste found the problem)

Baste >

Well … It is the problem. I thought that the GameManager (in new scene) was destroyed by the singleton, but not (or not fast enough to avoid Start() & Awake()).
The problem now, is to find a way to edit GameManager (deep structure) without having it in the scene.
I know that I can put it in the scene, apply it and remove it before running, but it’s not really a good way to do it (My team need some “none technical editing tools”).

May be I could remove all GameManager, in all levels at runtime ?! The new SceneManager could be a great tool for that.

Do you have any idea ?

thanks for your help

I have the exact same problem. Anyone any ideas?

Hi,
it was a long time ago …
I discovered (it’s write somewhere in Unity doc) that a singleton must be at the root of the hierarchy.
If not, it’s not working properly.

Is it your case ?

++

No, that didnt work, but I found a way around it tho: I had a few public static Dictionaries that kept throwing errors because they kept being re-populated on Awake, so I turned them into regular public Dictionaries and access them via static methods instead. Works like a charm :slight_smile: