{
bool GameEnd = false;
public float restartDelay = 1f;
public void GameOver()
{
if(GameEnd == false)
{
GameEnd = true;
Debug.Log("GAMEOVER");
Invoke("Restart", restartDelay);
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
I want to invoke "restart" with a delay but it shows "it couldn't be called"
what's wrong?
Declare both methods in the class scope: using System.Collections.Generic; public class YourClass: MonoBehaviour { //presumably monobehaviour
bool GameEnd = false;
public float restartDelay = 1f;
private IEnumerator coroutine;
void Start() {
coroutine = Restart();
}
public void GameOver()
{
if(GameEnd == false)
{
GameEnd = true;
Debug.Log("GAMEOVER");
Invoke("Restart", restartDelay);
}
StartCoroutine(coroutine);
}
IEnumerator Restart()
{
yield return new WaitForSeconds(1f);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Not debugged, exmaple just to show you how it works.
Your method is local (== nested) within the GameOver
method.
In order to use Invoke
it needs to be on class level scope like
bool GameEnd = false;
public float restartDelay = 1f;
public void GameOver()
{
if(GameEnd == false)
{
GameEnd = true;
Debug.Log("GAMEOVER");
Invoke(nameof(Restart), restartDelay);
}
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
otherwise Unity won't find it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.