How to unload a level after leaving a trigger area?

I have a game that will have several massive levels that flow right into each other (it’s a Metroidvania game), and so I need to unload levels when the character leaves those areas, so that it doesn’t crash the game due to using too much memory.

I’ve already tried:

void OnTriggerExit2D(Collider2D coll)
    {
         SceneManager.UnloadScene(sceneIndex);
    }

However, I read somewhere that you can’t call UnloadScene from physics triggers for some reason. Unity - Scripting API: SceneManagement.SceneManager.UnloadScene

But they say to use UnloadSceneAsync, which doesn’t exist. The link in the documentation is broken and my program won’t compile when I try to use it.

How to go about this? How does one unload a scene after the character leaves it?

Something like this (untested):

void Update()
{
     if(flag)
    {
        //prevents multiple calls to UnloadScene
        flag = false;
        SceneManager.UnloadScene(sceneIndex);
    }
}


 void OnTriggerExit2D(Collider2D coll)
     {
          flag = true;
     }

Not the prettiest but it should work.