- Home /
Invoke does not work
Im pretty new to unity. Im trying to make the code to wait 2 seconds and then restart the game. I have tried Invoke but it says "Trying to Invoke method: GameManager.Restart couldn't be called.". Is it any other way to make the script wait? here is my code:
using UnityEngine; using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{ public GameObject completeLevelUI; public float restartDelay = 1f; bool gameHasEnded = false;
public void CompleteLevel()
{
completeLevelUI.SetActive(true);
}
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Invoke("Restart", (restartDelay));
}
void Restart ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
Answer by Casiell · Jan 20, 2020 at 02:48 PM
Looks like you declared your Restart method inside EndGame method. Don't do that (I mean do that, but only when you really know what that does and do it intentionally), declare it in your GameManager class
oh thanks didnt see i declared it inside the endgame. Thanks.
Your answer
Follow this Question
Related Questions
Quick question about destroying bullets/objects 1 Answer
How do I wait for an object to load before accessing it's information? 1 Answer
WaitForSeconds Problem [C#] 4 Answers
WaitForSeconds Question 2 Answers
Animator.GetCurrentAnimatorStateInfo(0).IsName("FallOffBed") unexpectedly returns false 0 Answers