Restart Delay

I have the code below, but it doesn’t have a delay even though there should be.

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

public class GameManagerCubic : MonoBehaviour
{
    bool gameHasEnded = false; 

    public float restartDelay = 2f;


public void EndGame()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log("OVER OVER");
            Invoke("Restart", restartDelay);
        }
    }

    void Restart ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    

}

Sounds like you’re destroying the game object the script is attached to after calling “EndGame”. Make sure this isn’t the case:

In Play mode, select the game object with the script attached. Then when “EndGame” gets triggered, make sure it remains in the scene hierarchy.

Instead of using the Invoke function, have you thought of using a coroutine?

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

 public class GameManagerCubic : MonoBehaviour
 {
     bool gameHasEnded = false; 
 
     public float restartDelay = 2f;
 
 
 public void EndGame()
     {
         if (gameHasEnded == false)
         {
             gameHasEnded = true;
             Debug.Log("OVER OVER");
             Restart();
         }
     }
 
     void Restart ()
     {
                 StartCoroutine("Restart2");
     }
     IEnumerator Restart2() 
     { 
                 yield return new WaitForSeconds(restartDelay);
                 SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 }

I’m no expert, but I’m pretty sure that should fix it! Please tell me if you recieve any errors.