2016-03-28 59 views
-2

我有一段代碼用的並非所有的代碼路徑返回一個值:團結

錯誤編譯「並非所有的代碼路徑返回一個值」

我不知道如何解決這個問題。有任何想法嗎?這是我的代碼。

bool EnemyIsAlive() 
{ 
    searchCountdown -= Time.deltaTime; 
    if (searchCountdown <= 0f) 
    { 
     searchCountdown = 1f; 
     if (GameObject.FindGameObjectWithTag("Enemy") == null) 
     { 
      return false; 
     } 
    return true; 
    } 
} 
+0

的可能的複製並[c#返回錯誤「不是所有的代碼路徑返回一個值」](http://stackoverflow.com/questions/21197410/c-sharp-returning-error-not-all-code-路徑返回-a值) – Serlite

回答

1

EnemyIsAlive()應爲所有可能的條件返回一個布爾值;在你的情況下;如果if (searchCountdown <= 0f)評估爲false,該方法將不會返回任何內容。所以你需要爲false條件添加一個return語句。根據您正在處理的情況,它可能爲true/false,但回報應該在那裏。

bool EnemyIsAlive() 
{ 
    searchCountdown -= Time.deltaTime; 
    if (searchCountdown <= 0f) 
    { 
     searchCountdown = 1f; 
     if (GameObject.FindGameObjectWithTag("Enemy") == null) 
     { 
      return false; 
     } 
    return true; 
    } 
    return false; // one line added to solve the error 
} 
相關問題