2016-02-10 47 views
-4

這裏是我的功能:我如何在C#中統一解決這個問題

public void PowerupCollected(int AddScore) 
{ 
    score += AddScore; 
    scoreGUI.text = "lol"+score; 
} 

這是我如何調用該函數:

if(other.gameObject.name == "Powerup(Clone)") 
{ 
    control.PowerupCollected(); 
} 

這裏是錯誤消息

錯誤CS1501:沒有過載方法`PowerupCollected'需要0個參數

有什麼問題?是否因爲我在調用函數時在括號中不包括AddScore

+10

你有沒有讀過你的錯誤信息?如果是這樣,請再閱讀一次。 –

+0

*是否因爲我在調用函數時未在括號中包含AddScore *答案爲是。 – tchelidze

+0

你也可以聲明這樣的函數:** public void PowerupCollected(int AddScore = 0){} **,使參數變爲可選 –

回答

1

無論是AddScore參數添加到您的調用(比如control.PowerupCollected(42);或使參數可選:public void PowerupCollected(int AddScore = 0)

由於第二個解決方案是沒有意義的,你的情況,我會先用一個

0

你的函數調用應包括你要添加的分數的量:

if(other.gameObject.name == "Powerup(Clone)") 
{ 
    control.PowerupCollected(); 
} 

應該是(例如):

if(other.gameObject.name == "Powerup(Clone)") 
{ 
    control.PowerupCollected(10); 
} 

這會在您的分數上加10。

+0

謝謝你們!兩人都做了這項工作。 –

+0

如果您發現我們的答案是正確的,請接受該答案作爲此問題的正確答案:) – Tom