2015-09-10 21 views
1

我寫我的代碼的權利並基於碼(統一文檔)waitforseconds()方法行不通不工作

,但仍然沒有工作,沒有等待,任何幫助!

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 ! 
    } 
+0

可能_Assets/Resouces/win.mp3_找不到。控制檯中有任何錯誤? – Kay

+0

調用Somefunction()開始()然後檢查? –

+0

@Kay不,它存在100% –

回答

0
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()

0

你需要寫的邏輯是決定玩家獲勝。之後:

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