2016-10-07 155 views
2

在我的遊戲中,我有主菜單,通過它我開始新遊戲(加載另一個場景)並執行下面的腳本。但是當我回到主菜單並再次開始新遊戲時,下面的腳本不起作用。我試圖阻止啓動函數內部協同程序,但它並沒有幫助協程不會第二次工作

private IEnumerator OpenCloseEyesAnimation() 
{ 
    if (!isOpenEyesAnimationStarted) 
    { 
     isOpenEyesAnimationStarted = true; 
     openCloseEyes.OpenCloseDoor(); // this work 1st and 2nd time 
     yield return new WaitForSeconds(6.0f); // this line don't work 2nd time 
     stepNumber++; 
     isOpenEyesAnimationStarted = false; 
    } 
} 

有一次,我把它叫做更新功能

enter image description here

+1

你在哪裏調用這個函數? – Nestoraj

+1

如果你在6秒內打開和關閉菜單(bool還沒有被重置),你是否確定要在進入主菜單時重置布爾值或者在進入主菜單之前等待超過6秒鐘? –

+0

@Nestoraj我把它稱爲更新一次 – dima

回答

3

裏面你應該需要在適當的地方使用StopCoroutine。每次啓動程序時,先停下來。

private IEnumerator OpenCloseEyesAnimation() 
    { 
     if (!isOpenEyesAnimationStarted) 
     { 

      isOpenEyesAnimationStarted = true; 
      openCloseEyes.OpenCloseDoor(); 
      yield return new WaitForSeconds(6.0f); // this line don't work after second launch 
      stepNumber++; 
      isOpenEyesAnimationStarted = false; 
     } 
    } 

由於Commment提(@Paradox鍛造)更新您的UpdateEvent代碼:

case 3: 
    StopCoroutine(OpenCloseEyesAnimation()); 
    isOpenEyesAnimationStarted = false; 
    StartCoroutine(OpenCloseEyesAnimation()); 
+2

如果在第二次調用isOpenEyesAnimationStarted時它仍然被設置爲true,則不起作用 –

+0

不,它不起作用並且isOpenEyesAnimationStarted = false。 WaitForSeconds之前的行在第二次工作 – dima

+1

@dima檢查更新的答案。 –