这几乎是我第一次使用 C Sharp 和 Unity。我试图统一使用 invoke() 函数,但它给出了错误
“尝试调用方法:无法调用 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
a 本地方法 https://learn.microsoft.com/dotnet/csharp/programming-guide/classes-and-structs/local-functions嵌套在另一个方法中endgame
.
Afaik MonoBehaviour.Invoke https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html只能调用类级别的方法。
-
这也是这里或原始代码中的拼写错误,但是Restart1
不只存在Restart
。为了避免基于名称的代码中的拼写错误,我会使用nameof https://learn.microsoft.com/dotnet/csharp/language-reference/operators/nameof
你的代码应该看起来像
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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)