2016-11-16 28 views
-3

我試圖比較場景名稱,但我收到以下錯誤:如何獲得並比較當前場景名稱

錯誤CS0019:運營商==' cannot be applied to operands of type 方法組「和'串」

我無法弄清楚如何解決這個錯誤。請幫幫我!錯誤位於箭頭

void CheckCurrentLevel() 
{ 
    for(int i = 1; i < LevelAmount; i++) 
    { 
    ---> if (SceneManager.LoadScene == "Level" + i) { 
      CurrentLevel = i; 
      SaveMyGame(); 
     } 
    } 
} 
+0

一個是方法,一個是'string' – Danh

+0

請更新您的問題,以反映實際的** **回答您選擇,讓其他人誰遇到同樣的問題,因爲你找到了解決這個問題的方法。 – spender

回答

1

SceneManager.LoadScene是用於加載的場景的void功能。它不會返回任何內容,因此您無法將其與字符串進行比較。

它看起來像你想的當前場景name"Level" + i比較。如果這是真的,那麼你正在尋找SceneManager.GetActiveScene().name

void CheckCurrentLevel() 
{ 
    for (int i = 1; i < LevelAmount; i++) 
    { 
     if (SceneManager.GetActiveScene().name == "Level" + i) 
     { 
      CurrentLevel = i; 
      SaveMyGame(); 
     } 
    } 
} 
+1

謝謝!這對我有效。 –

0

請記住,==運營商檢查,如果兩個操作數的值是否相等,受條件是兩個操作數應該是同一類型。從錯誤消息很明顯,LoadScene被定義爲一種方法。

if (SceneManager.LoadScene(params if any) == "Level" + i) 
{ 
    // Code here 
} 

符合條件是LoadScene()方法應該返回一個字符串對象否則返回類型將是這個業務對象:如果你使用這樣

您的代碼將工作正常情況下使用一個字符串屬性進行比較

相關問題