How can i make a function to execute instructions after use of SceneManager.LoadScene(),

Im working on an endless runner game and i encountered a problem with my “restart / try again” system.
Basically, when the player dies, the “GameOver” menu appears, along with the “try again” button that should reset the scene and make the player start all over again. However, after calling SceneManager. LoadScene() I wanted to disable the “MainMenu Panel” too, as I don’t want the player to see the main menu every time he dies, but it seems that my function ignore everything after .LoadScene() is called.
here is the code :

public GameObject MenuPanel;
public GameObject ScorText;

public void Restart()
    {
        SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
        // MenuPanel is always active at the start of the game. It is set to inactive once Play button is pressed. 
        MenuPanel.SetActive(false);  
        ScorText.SetActive(true);

    }

How can i solve it ?

,

After a scene is loaded, all gameobjects in the previous scene are unloaded, including scripts, unless the script is attached to an object set to DontDestroyOnLoad. For example:

 public void Restart()
     {
         // Keeps script loaded
         DontDestroyOnLoad(gameObject);
         SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
         // MenuPanel is always active at the start of the game. It is set to inactive once Play button is pressed. 
         MenuPanel.SetActive(false);  
         ScorText.SetActive(true);
 
         // Unloads after the function has finished
         Destroy(gameObject);
     }

Or use a callback to a static script. They stay active throughout all scenes.