SceneManager is duplicating scenes

Recently, I noticed a weird behavior on my loading screen. For some strange reason, it’s duplicating the scene to load (it’s loading the same scene twice). I don’t really understand why this is happening.

This is how my loading process works:
The loading screen itself is isolated in its own scene. I use an event system (Game Events approach) to call the loading screen when I need it.
My game has a boot scene. This scene only contains a script that solves some future issues of the session and loads the loading screen.

When a new scene is loaded, the other one (the scene in which the loading screen was called) is unloaded with this code:

Application.backgroundLoadingPriority = ThreadPriority.Normal;

        loadingOperation = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Additive);

        int _sceneCount = SceneManager.sceneCount;
        for (int i = 0; i < _sceneCount; i++)
        {
            Scene scene = SceneManager.GetSceneAt(i);
            if (scene.name != "LoadingScreen")
            {
                SceneManager.UnloadSceneAsync(scene);
                yield return null;
            }
        }

Next, we’ll wait until the loading is done:

 while (!loadingOperation.isDone)
        {
            yield return null;
        }

After that, I set the newly loaded scene as the active scene:

SceneManager.SetActiveScene(SceneManager.GetSceneByName(levelName));

And I end the coroutine with a yield return break;

Now, once the boot scene is unloaded, the loading screen take us to the main menu, where the user can start the game and do other things. To call the loading screen (in other scenes after boot), I use a Loading Screen Caller. It literally only calls the loading screen to load a new scene:

[SerializeField] private StringDataType setString;
    [SerializeField] private GameEvent eventToCall;

    public void LoadNewLevel(string levelName) {
        setString.SetStringValue(levelName);
        eventToCall.Raise();
    }

Since the Loading Screen scene is already loaded, it can “hear” that the event was raised, and therefore, load a new scene.

But when I load the next scene from the main menu, the scene in question duplicates itself and the scene manager load both of them. It’s weird, since I use the same logic for loading from the boot screen, and that doesn’t happen from there.

This is strange, since this doesn’t happen on other projects, and they use pretty much the same logic.

How and when are you subscribing to events?
Are you remembering to unsubscribe at the appropriate time? Not 100% but from your description maybe you have two active subscriptions to the event that causes you to load the scene twice.

Good observation, I forgot to mention that. The event subscribing is done on the loading screen itself:

void OnEnable() {
    gameEvent.Register(this);
}

void OnDisable() {
     gameEvent.Unregister(this);
 }

 public void OnEventRaised() {
      LoadScene(readString.GetStringValue());
 }

The loading screen is the event listener. Now, the event in question is raised by the Loading Screen Caller, as I mentioned before (well, it’s on the code).

Hard to say really from the limited information here.

The way I’d approach figuring this out is to attach the debugger and set some breakpoints.

The first place I’d put a breakpoint is here:

loadingOperation = SceneManager.LoadSceneAsync(levelName, LoadSceneMode.Additive);

Check if that’s getting called twice. If it is, pay careful attention to the stack trace at that time to figure out what code is calling it. If it’s not getting called twice, well that tells you a lot too - namely that some other code is loading the duplicate scene. That should help you start to unravel what’s going wrong.

So, I did as you say. This were the results:
On the first iteration, from the boot scene to the main menu:
7436222--911147--results1.png

Effectively, there are two scenes at the moment (boot and loading screen), until boot is unloaded at:
7436222--911150--results2.png

Next screen is the main menu. I hit on play, and apparently I got the same results as the first iteration:
7436222--911153--results3.png

Except for one thing that I’m seeing here. The AsyncOperation loadingOperation is not null anymore. Not sure if that could be it.

Now, when the script is loading the next scene, it stays at line 51 (unloading a scene). If I hit on Continue, it repeats the same line for about 3 times, until it finishes (The loading screen does a similar behavior to when the boot screen is moving to the main menu screen. It unload the previous scene while loading the next one. Once the other scene is unloaded, the next scene is loaded. However, when I’m loading from the main menu, it unloads the main menu, but loads 2 new scenes, instead of just one). Maybe that’s where the issue is happening. But, I’m not sure why UnloadSceneAsync would be duplicating scenes.

Sorry about the info, I didn’t think this was complicated. More info:

I’m working on Unity 2019.4.29f1, on Android platform. Loading Screen Caller is the only script that calls the loading screen (raises the event). Boot screen also raises the event, however, this scene is unloaded at the beginning. The event can also be raised multiple times, but again, only Loading Screen Caller calls this event.

This is the boot screen script:

void Start() {
     StartCoroutine(Boot());
}

public void LoadScene() {
     loadingScreen.Raise();
}

    IEnumerator Boot() {
        sceneName.SetStringValue(firstScene);
        SceneManager.LoadScene("LoadingScreen", LoadSceneMode.Additive); //This scene is automatically unloaded
        yield return null; //Wait a frame before procceding
        LoadScene();

        yield break; //Kill the coroutine. Initialize game
}

Update: it seems that it disappeared…out of nowhere. And for pretty much no reason. The only change I recall doing was changing the lightning settings to Auto Generate. I don’t think it’s related at all…

However, it seems it only went away on editor, but not on build. It definitely seems like a bug.