error CS1736: ‘bulletSpeed’ 的默认参数值必须是编译时常数

huangapple go评论130阅读模式
英文:

error CS1736: Default parameter value for 'bulletSpeed' must be a compile-time constant

问题

以下是翻译好的内容:

我正在尝试使用Unity 2D创建一个游戏。以下代码抛出了一个错误:“error CS1736: 'bulletSpeed'的默认参数值必须是编译时常量”,而说实话,我无法弄清楚如何修复它。以下是有问题的代码:

 private Rigidbody2D rb;
    private MovementController checkForSlowMotion;
    private float bulletSpeednew;
    //private float bulletSpeed;

    // Start is called before the first frame update
    void Awake()
    {
        checkForSlowMotion = GameObject.Find("Player").GetComponent<MovementController>();
        rb = GetComponent<Rigidbody2D>();
    }

    public void MoveBullet(float bulletSpeed = bulletSpeednew)
    {
        if (checkForSlowMotion.slowDown == true)
        {
            this.bulletSpeed = 1.0f;
        }
        else if (checkForSlowMotion.slowDown == false)
        {
            bulletSpeed = 10.0f;
        }
        rb.MovePosition(transform.position + transform.right * bulletSpeed * Time.deltaTime);
    }

提前致谢。

我尝试将非静态变量“bulletspeednew”作为“bulletSpeed”传递,但未成功。

英文:

I am trying to create a game with Unity 2D. The following code throws an error "error CS1736: Default parameter value for 'bulletSpeed' must be a compile-time constant" which, quite frankly, I can't figure out how to fix.
Here's the guilty code:

 private Rigidbody2D rb;
    private MovementController checkForSlowMotion;
    private float bulletSpeednew;
    //private float bulletSpeed;

    // Start is called before the first frame update
    void Awake()
    {
        checkForSlowMotion = GameObject.Find(&quot;Player&quot;).GetComponent&lt;MovementController&gt;();
        rb = GetComponent&lt;Rigidbody2D&gt;();
    }

    public void MoveBullet(float bulletSpeed = bulletSpeednew)
    {
        if (checkForSlowMotion.slowDown == true)
        {
            this.bulletSpeed = 1.0f;
        }
        else if (checkForSlowMotion.slowDown == false)
        {
            bulletSpeed = 10.0f;
        }
        rb.MovePosition(transform.position + transform.right * bulletSpeed * Time.deltaTime);
    }

Thank you in advance.

I have tried passing a non-static variable "bulletspeednew" as "bulletSpeed", unsuccessfully.

答案1

得分: 1

如Ron在他的评论中提到的,C#不允许将非常量变量(即在运行时可以更改其值的变量,如Unity中的序列化字段)用作函数签名中的默认值,我认为在这里最好的选择是使用重载来设置“默认”值。

public void MoveBullet()
{
    MoveBullet(bulletSpeednew);
}

public void MoveBullet(float bulletSpeed)
{
    //...
}
英文:

As Ron mentioned in his comment, C# doesn't allow none-const variables (IE variables whose value can change during runtime, like serialized fields in Unity) to be used as default values in function signatures, I think your best bet here would be to set the "default" value with an overload.

public void MoveBullet()
{
    MoveBullet (bulletSpeednew);
}

public void MoveBullet(float bulletSpeed)
{
    //...
}

huangapple
  • 本文由 发表于 2023年3月12日 18:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75712582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定