LoadSceneAsync is freezing game while loading.

I thought the point of loading something Async is that it will load in the background while the game keeps on doing it’s thing. However my game just freezes for 1-2 seconds while the next scene loads?

I realized my gameworld was too taxing since it took 10-ish seconds to load, so I found out about streaming scenes and now I have this issue instead… Is it not possible to make large worlds in Unity?

IEnumerator LoadYourAsyncScene()
    {
        // The Application loads the Scene in the background at the same time as the current Scene.
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(_neighbor, LoadSceneMode.Additive);
        Debug.Log("Loading Scene: " + _neighbor);

        //Wait until the last operation fully loads to return anything
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        StartCoroutine(LoadYourAsyncScene());
    }

Is there a mistake in my code? How can I load a “neighboring” scene in LoadSceneMode.Additive without my game freezing?

I have a total of 6370 GameObjects in the scene, and a lot of them do bitmasking in Start(), so loading the entire scene in one go is not feasible.

I think it only loads the game objects, it doesn’t run their Start methods asynchronously… so if you have scripts that are taking time, that might account for your 1-2 seconds. Still, it’s down from 10 :slight_smile: Perhaps you can profile it and find out what’s the matter and (if possible) fix it…

1 Like

The only thing I could change is to start using a 2d array that does the bitmasking, instead of my current system that uses raycast (raycasts all 8 directions around the tile to check for neighboring bitmask objects). I’m assuming my way is a lot more taxing than if you just check for adjacent elements in a 2d array. But it hurts my head to even think about how much re-work that would require :stuck_out_tongue:

Fair enough, then you should profile it… confirm that’s your issue… if it is and it’s a headache you don’t want, i suppose you just have to live with it?

1 Like

Yup that seems to sum it up pretty well ^^

EDIT: One other idea I have is to keep this one “heavy” scene alive throughout the games entire lifetime, and load/unload every other scene additive. And just activate/deactivate the heavy scene instead of loading/unloading it. Since my game runs at >1000 fps I could prob get away with this, its just the start() that is a bit heavy, but I could get that down to being a 1-off.