Problems with changing scene

Hi folks! I meet new problem, and can’t figure it self.

I want to have level system and change levels(scenes) of my game when I want. But seems it to much for me.
I have few scenes for now MenuScene, no problem with it. Next I has a GameplayScene, in this scene I store player, enemies, UI and other object which should be on any level. Of course I have Level1Scene, and LoadScreenScene. I watch some guides and read forums, but don’t understand a bit.

Run app → MenuScene, click on start game button → LoadScreenScene → GameplayScene → Level1Scene.Additive

I thinks this like it should be, but I don’t sure…

Now I use SceneLoader class based on MonkeyCode guide

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public static class SceneLoader
{
    private class LoaderMonoBehavior : MonoBehaviour
{
   
}

    public enum Scene
    {
        Menu,
        Loading,
        Gameplay,
        Level1,
    }

    private static Action onLoaderCallback;
    private static AsyncOperation loadingAsyncOperation;
    //public Player player;
    public static int method = 0;

    public static void Load(Scene scene, int method)
    {
        onLoaderCallback = () =>
        {
            GameObject loadingGameObject = new GameObject("Loading Game Object");
            loadingGameObject.AddComponent<LoaderMonoBehavior>().StartCoroutine(LoadSceneAsync(scene, method));
        };

        SceneManager.LoadScene(Scene.Loading.ToString(), 0);
    }

    private static IEnumerator LoadSceneAsync(Scene scene, int method)
    {
        yield return null;

        switch(method)
        {
          
            case 0:
                loadingAsyncOperation = SceneManager.LoadSceneAsync(scene.ToString());
                Debug.Log("Load scene " + scene.ToString() + " method: " + method);
                break;
            case 1:
                loadingAsyncOperation = SceneManager.LoadSceneAsync(scene.ToString(), LoadSceneMode.Additive);
                Debug.Log("Load scene " + scene.ToString() + " method: " + method);
                break;
        }

       

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

    public static float GetLoadingProgress()
    {
        if(loadingAsyncOperation != null)
        {
            return loadingAsyncOperation.progress;
        }
        else
        {
            return 1f;
        }
    }

    public static void LoaderCallback()
    {
        if(onLoaderCallback != null)
        {
            onLoaderCallback();
            onLoaderCallback = null;
        }
    }
}

It works well if I want simply load scene, but when I try to load scene in additive mode… As you can see LoadingScreen broke all. Because it loads on every load. And with this code we get that load order Menu->LoadScreen->Gameplay->LoadScreen->Level1.additive, as you can see I can’t save gameplay scene, help me to find right way to do this or maybe use another way to manage my levels. Thanks.

Hey,

So in a nutshell, the Loader scene always replaces the current scene, then the new scene is either loaded with Single or Additive, but in the latter case the Loader isn’t replaced by the new scene, so you need to explicitly do so.

At the end of the LoadSceneAsync method, add the bolded code.

while (!loadingAsyncOperation.isDone)
        {
            yield return null;
        }
        // New code
        if (method == 1)                
               SceneManager.UnloadScene(Scene.Loading.ToString())

Also, don’t use integers for something that already has an enum provided: replace 0 with LoadSceneMode.Single and 1 with LoadSceneMode.Additive for readability.

– Lucian