我正在制作2D游戏,想在玩家的所有生命耗尽后造成大约3秒的延迟。我试图在场景重新开始之前实现Coroutine方法,但它不起作用。我已经实现了协程方法,每当我的玩家从悬崖上坠落并重新回到它的位置时。它就像护身符一样起作用。
public void Respawner()
{
StartCoroutine("RespawnCoroutine");
}
// Coroutine Delay of 2 sec for each time player Respawn
public IEnumerator RespawnCoroutine()
{
classobj.gameObject.SetActive(false);
yield return new WaitForSeconds(respawnDelaySec);
classobj.transform.position = classobj.respawnPoint;
classobj.gameObject.SetActive(true);
}
public void ReduceLives()
{
if (lives <= 3 && lives >= 2)
{
lives--;
live_text.text = "Remaining Live " + lives;
}
else
{
StartCoroutine("RestartScene1");
}
}
public IEnumerable RestartScene1()
{
yield return new WaitForSeconds(RestartSceneDelaySec);
SceneManager.LoadScene("demo2");
}
控制台窗口上没有错误,但从未调用过SceneManager.LoadScene("demo2")
;,并且每次我死后和剩余1次生命后,播放器都会重新启动
发布于 2018-06-02 13:18:45
第二个协程的问题是..
您错误地使用了"IEnumerable“而不是"IEnumerator",请将其更改为"IEnumerator”,它将正常工作。
发布于 2018-06-02 07:40:33
不应该在StartCoroutine("RestartScene1");
之后调用SceneManager.LoadScene("demo2");
。
StartCoroutine("RestartScene1");
你可以说这段代码是一个异步代码。它被调用,程序的执行继续进行(执行不会在这里等待)。在yield
ing之后,你应该在协程中调用你想要延迟的代码。
小例子:
public void SomeFunction()
{
StartCoroutine("RestartScene1");
// The code here will **not** be delayed
}
public IEnumerable RestartScene1()
{
yield return new WaitForSeconds(RestartSceneDelaySec);
// The code here will be delayed
}
https://stackoverflow.com/questions/50652136
复制相似问题