我真的需要帮助,当我将 Invoke 放入 C# 中时,出现以下错误:
声明了本地函数“RestartGame”但从未使用过 Assembly-CSharp
我真的不知道为什么会发生这种情况,但这是代码:
使用Unity引擎;
使用UnityEngine.SceneManagement;
公共类 GameManager :MonoBehaviour
{
布尔游戏已结束 = 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手册:为了更好的性能和可维护性,请使用协程。https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html 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 https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)