[英]i can't invoke a function directly
{
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;在 class scope 中声明这两种方法:使用 System.Collections.Generic; public class YourClass: MonoBehaviour { //presumably monobehaviour
public class YourClass: MonoBehaviour { //大概是 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.未调试,exmaple 只是为了向您展示它是如何工作的。
Your method is local (== nested) within the GameOver
method.您的方法在
GameOver
方法中是本地的(== 嵌套)。
In order to use Invoke
it needs to be on class level scope like为了使用
Invoke
,它需要在 class 级别 scope 上
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.否则 Unity 将找不到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.