Im trying to make it so when the game ends it puts you back to the level you were on I currently have two levels and its not working. Heres my code:

using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
bool gameHasEnded = false;
bool level2 = false;
public float restartDelay = 1f;
public float winDelay = 8f;
public GameObject completeLevelUI;
public void EndGame()
{
if(gameHasEnded == false)
{

            gameHasEnded = true;
            Debug.Log("Oops");
            Invoke("Restart",restartDelay);
        }
        

    }

    void Restart()
    {
        
        if(level2 == true)
        {
            SceneManager.LoadScene("Scene2");
   
        }
        if (level2 == false)
        {
            SceneManager.LoadScene("Scene1");
        }

    }
    void Level2()
    {
        SceneManager.LoadScene("Scene2", LoadSceneMode.Single);
        SceneManager.SetActiveScene(SceneManager.GetSceneByName("Scene2"));
        SceneManager.UnloadSceneAsync("Scene1");
    }
    public void WinGame()
    {
        completeLevelUI.SetActive(true);
        Debug.Log("WonGame");
        Invoke("Level2", winDelay);
        level2 = true;
    }
    
}

If you’re just loading the level you’re already on you can just do

public void ResetScene()
{
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

Then just call ResetScene when the game ends.
You can add the async crap into there too.