waitforseconds()方法不起作用不起作用

waitforseconds() method not work not working

我根据(Unity 文档)中的代码正确编写了我的代码

但仍然无法正常工作,没有等待,任何帮助!

  IEnumerator wait(float waitTime)
  {
      Debug.Log("Wait"); // write this successfully
      yield return new WaitForSeconds(waitTime);
      Debug.Log("Waitting"); // not write it , not enter here !!
  }

  void somefunction()
  {
      StartCoroutine("wait", Resources.Load<AudioClip>("win").length);
      Debug.Log("waiting finished"); // write it directly !
  }
void Update () {

    if (Input.GetKey ("down")) { // Make here Game Wining condition
        win = true; // Make Bool variable true
    }
    if (win)
        tim += Time.deltaTime;//play here audio

    if (tim > 6.0) { // When getting finish the audio, load the level
        Debug.Log ("Load");
    }
}

另一种解决方案 无需工作 waitforseconds()

你需要编写决定玩家获胜的逻辑。之后:

void decidePlayerWin(){
  /// Do your game logic and if player wins/loses etc call 
   StartCoroutine("wait", Resources.Load<AudioClip>("win").length);
}

IEnumerator wait(float waitTime)
{
    Debug.Log("Wait"); // write this successfully
    yield return new WaitForSeconds(waitTime);
    Application.LoadLevel(Application.loadedLevel);
}

您可以在 Update 方法中应用您的逻辑。

喜欢:

void Update(){
    //Do logic and decide wins/loses.
    if(playerWins){
       StartCoroutine("wait", Resources.Load<AudioClip>("win").length);
    }
}

PS:你需要了解Coroutines