Connor Propp
Connor Propp

Reputation: 13

Can't get Invoke() to work

//Tring to add a delay before game restarts

using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {

    bool GameEnded = false;

    public float RestartDelay = 4f;

    public void CompleteLevel ()
    {
        Debug.Log("1!");
    }

    public void GameOver ()
    {
        if (GameEnded == false) 
        {
            GameEnded = true;
            Debug.Log("Game Over");
            Invoke("Restart", RestartDelay);
            Restart();

        }
    }

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

Upvotes: 0

Views: 91

Answers (1)

I.B
I.B

Reputation: 2923

You use Invoke to call Restart() after a certain delay but then you directly call Restart() which will load the next scene.

What Invoke does is allow you to schedule method calls to occur after a certain time. You seem to be using it as a function that will make the process wait a certain amount of time which is wrong.

Simply remove the Restart() call after Invoke("Restart", RestartDelay);

Upvotes: 1

Related Questions