【发布时间】:2020-06-28 22:57:52
【问题描述】:
Image of my code in Visual Studio
请原谅,我对编码有点陌生,所以也许这是一个愚蠢的问题。
我正在学习如何使用 Unity,并且我正在使用 Visual Studio 来编辑我的代码 (C#)。 出于某种原因,大多数错误不会出现红色波浪线。它甚至不会承认有错误。
我让它识别的唯一错误是缺少分号。
例如,我可以说一个字符串等于一个浮点数(如下所示:characterName = itemDurability;),它在 Visual Studio 或 VSCode 中都没有问题。我可以将一个项目定义为多个不同的事物,但仍然没有错误。当然 Unity 会在代码加载时告诉我有问题,但我想在编写时知道。
我已尝试更新和重新安装,但没有任何效果。我在网上找不到任何可以帮助我解决这个问题的东西。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
string characterName;
characterName = "Duncan";
int characterLevel = 5;
int experience = characterLevel * 5;
float itemDurability = 1.527f;
experience = (int)itemDurability;
characterName = itemDurability;
bool equippable = false;
if (itemDurability > 1f)
{
experience = (int)(itemDurability * 1.5f) / characterLevel;
}
}
}
【问题讨论】:
-
向我们展示您正在谈论的代码,以及您期望的错误。这太模糊了。
-
可能是代码文件在项目属性中没有设置为编译,而是作为内容添加的。很难说没有任何额外的信息。
-
@Igor 他们说缺少分号被检测为错误,这向我表明该文件已设置为编译。
-
好的,您希望编译器在这段代码中引发什么错误?
-
itemDurability 是一个浮点数,但随后您尝试将其转换为一个 int 和一个字符串。那是怎么回事? characterName 与 itemDurability 有什么关系?
标签: c# unity3d visual-studio-code syntax-error