本文介绍了invoke() 函数统一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这几乎是我第一次使用 C 锐利和统一.我正在尝试统一使用 invoke() 函数,但它给出了错误
Its almost my first time using C sharp and unity. I am trying to use invoke() function in unity but its giving the error
尝试调用方法:无法调用 EndGame.Restart1."
public class EndGame : MonoBehaviour
{
bool GameHasEnded = false;
public float Timer = 1f;
public void endgame()
{
if (!GameHasEnded)
{
GameHasEnded = true;
Debug.Log("GameOver");
Invoke("Restart", Timer);
}
void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
推荐答案
您正在尝试
Invoke
本地方法 嵌套在另一个方法endgame
中.You are trying to
Invoke
a local method that is nested inside of another methodendgame
.Afaik
MonoBehaviour.Invoke
只能在类级别调用方法.Afaik
MonoBehaviour.Invoke
can only call methods on class level.它也可能是这里或您的原始代码中的一个错字,但
Restart1
不存在,只有Restart
.为了避免基于名称的代码中的拼写错误,我将使用nameof
It also is either a typo here or in your original code but
Restart1
doesn't exist onlyRestart
. To avoid typos in name based code I would usenameof
你的代码应该看起来像
public class EndGame : MonoBehaviour { private bool GameHasEnded = false; // Timer is strange name for that // I would suggest "Delay" public float Delay = 1f; public void endgame() { if (!GameHasEnded) { GameHasEnded = true; Debug.Log("GameOver"); // In general in order to avoid typos I would prefer to use "nameof" Invoke(nameof(Restart), Delay); } } private void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } }
这篇关于invoke() 函数统一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!