unity3d - 声明了本地函数“RestartGame”但从未使用过 Assembly-CSharp
问题描述
我真的需要帮助,当我在 C# 中调用 Invoke 时,出现以下错误:
声明了本地函数“RestartGame”但从未使用过 Assembly-CSharp
我真的不知道为什么会这样,但这里是代码:
使用 UnityEngine;使用 UnityEngine.SceneManagement;
公共类 GameManager : MonoBehaviour { bool gameHasEnded = false;
public float restartDelay = 2f;
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Invoke("RestartGame", restartDelay);
}
void RestartGame ()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
解决方案
unity manual:为了获得更好的性能和可维护性,请改用 Coroutines。 https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
尝试这样的事情:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
bool gameHasEnded = false;
public float restartDelay = 2f;
private IEnumerator coroutine;
public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
coroutine = RestartDelayed(restartDelay);
StartCoroutine(coroutine);
}
void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
IEnumerator RestartDelayed(float delay)
{
yield return new WaitForSeconds(delay);
RestartGame();
}
}
}
https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
推荐阅读
- c# - 升级到 PackageReference 后,Nuget Pack 失败并显示代码 NU5012
- python - 如何保持第一次出现在python数据框中重复的值?
- c# - 使用 C# 连接 MySQL Server 8.0
- python - 如何将geopandas图提取为由像素数值组成的numpy数组?
- tensorflow - 如何将操作结果分配给 Layer.kernel?
- metaprogramming - 如何动态添加类属性?
- excel - 从列表框用户窗体中删除一行
- php - PHP Shell_exec = 如何在网页上显示的表格中输出结果?
- google-cloud-platform - 如何使用 stackdriver 从对话流中检索对话数据?
- laravel - 示例 laravel 项目 error500