【问题标题】:How can I solve this issue in C# Unity [closed]如何在 C# Unity 中解决此问题 [关闭]
【发布时间】:2016-02-10 12:37:33
【问题描述】:

这是我的功能:

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

我是这样称呼这个函数的:

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

这是错误信息

错误 CS1501:方法 `PowerupCollected' 没有重载需要 0 个参数

怎么了?是不是因为我在调用函数时没有在括号中包含AddScore

【问题讨论】:

  • 曾经阅读过您的错误信息吗?如果是这样,请再读一遍。
  • 是不是因为我在调用函数时括号中没有包含 AddScore 答案是肯定的。
  • 你也可以这样声明函数:public void PowerupCollected(int AddScore = 0){} 使参数变为可选
  • 另一个程序员无法读取的实例:方法 `PowerupCollected' 没有重载需要 0 个参数

标签: c# function unity3d compiler-errors


【解决方案1】:

将 AddScore 参数添加到您的调用中(例如 control.PowerupCollected(42); 或将参数设为可选:public void PowerupCollected(int AddScore = 0)

由于第二个解决方案在您的情况下没有意义,我会使用第一个。

【讨论】:

    【解决方案2】:

    您的函数调用应包含您要添加的分数:

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

    应该是(例如):

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

    这将使您的分数增加 10。

    【讨论】:

    • 谢谢你们!两者都完成了工作。
    • 如果您发现我们的某个答案是正确的,请接受该答案作为该问题的正确答案:)
    猜你喜欢
    • 2011-02-24
    • 2020-06-15
    • 2014-04-26
    • 2020-02-16
    • 2019-10-26
    • 2023-02-23
    • 2020-10-29
    • 2021-06-12
    • 2021-11-05
    相关资源
    最近更新 更多