Abdulrahman KSA
Abdulrahman KSA

Reputation: 3

Cant call a method in Invoke

So I have this weird issue where I am trying to call a method in Invoke but it does not work for some reason IDK what am I doing wrong

public void EndGame ()
{
    if (GameHasEnded == false)
    {
      GameHasEnded = true;
      UnityEngine.Debug.Log("Game Over");
      Invoke("Restart",2f);
    }
    void Restart ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
}

Whenever I run this script I get a message

Trying to Invoke method: GameManager.Restart Couldn't be called"

Upvotes: 0

Views: 447

Answers (1)

derHugo
derHugo

Reputation: 90679

Why is your method nested inside the EndGame method?

It should rather be

public void EndGame ()
{      
    if (!GameHasEnded)
    {
        GameHasEnded = true;
        UnityEngine.Debug.Log("Game Over");
        Invoke(nameof(Restart), 2f);  
    }
}

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

Upvotes: 4

Related Questions