【问题标题】:How would i create a statement in which i effect 2 different variables in c#我将如何创建一个在 C# 中影响 2 个不同变量的语句
【发布时间】:2017-03-11 23:06:00
【问题描述】:

所以我正在创建一个游戏,我的脚本看起来不错,但出现了 2 个编译错误,这是脚本

    if (Input.GetKeyDown(KeyCode.C) && movementSpeed = 6.5f)
    {
        transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
        movementSpeed = crouchWalkSpeed;
    }
    if (Input.GetKeyUp(KeyCode.C) && movementSpeed = 3)
    {
        transform.localScale = new Vector3(1, 1, 1);
        movementSpeed = 6.5f;
    }

还有一个

public float movementSpeed = 6.5f;
float crouchWalkSpeed = 3;

所以我没有看到问题,但是错误说:


严重性代码描述项目文件行抑制状态 错误 CS0019 运算符“&&”不能应用于“bool”和“float”类型的操作数第一人称游戏。CSharp C:\Users\desha\Documents\First person game\Assets\Scripts\FirstPersonController.cs 52 Active


严重性代码描述项目文件行抑制状态 错误 CS0019 运算符“&&”不能应用于“bool”和“float”类型的操作数第一人称游戏。CSharp C:\Users\desha\Documents\First person game\Assets\Scripts\FirstPersonController.cs 57 Active

【问题讨论】:

  • if 语句中有布尔检查和浮点 = 检查。

标签: c# unity3d


【解决方案1】:

您需要在if 语句中使用两个==(比较运算符),而不是单个=(赋值运算符)。

类似这样的:

if (Input.GetKeyDown(KeyCode.C) && movementSpeed == 6.5f)

还有这个:

if (Input.GetKeyUp(KeyCode.C) && movementSpeed == 3)

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2012-10-20
    • 1970-01-01
    • 2016-07-11
    • 1970-01-01
    相关资源
    最近更新 更多