LoadSceneMode.Additive and FindObjectOfType

Hi guys,

I’m implementing a scene management module to load my levels in background.
The simplified situation is: I have a singleton in Scene1 that use the accessor bellow:

private static ScreenManager instance;
public static ScreenManager Instance
{
    get
    {
        if (instance == null)
            instance = FindObjectOfType<ScreenManager>();
        return instance;
    }
}

In Scene2, I have a GameObject that tries to access this singleton:

private void OnEnable()
{
    Debug.Log(ScreenManager.Instance);
}

Scene1 loads Scene2 with the additive flag and then the object in Scene2 tries to get the singleton in Scene1.

When I have only Scene1 loaded in the editor and I hit the Play button, everything works as expected.
When I have Scene1 and Scene2 loaded in the editor and I hit the Play button, ScreenManager.Instance is Null.

Is this the expected behavior? Can I avoid using a coroutine to fix this?

Thank you for reading this!

I know you are asking another question, but why you dont use SceneManager.LoadLevelAsync?

Add a debug log to your instance getter, see how many times its called, and report the instance value, it may be getting nulled by the ScreenManager itself getting destroyed.
You could try making a static reference and seeing if it changes.

I am using SceneManager.LoadLevelAsync, with the additive flag.

 SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);

I tried that and the instance doesn’t seem to be destroyed. The error happens the first time it is called, at the beginning of the game initialization.

I can’t statically link the objects as ScreenManager is in Scene1 and the other object is in Scene2.

Anyone?