【问题标题】:bool isItem(TextAsset f) always returns falsebool isItem(TextAsset f) 总是返回 false
【发布时间】:2016-11-18 20:18:55
【问题描述】:

以下脚本是我开始编写的,它需要一个 TextAsset 并拆分行并将其与各种字符串进行比较。但是我的检查功能只返回假。有什么我遗漏的主要内容吗,Unity 没有抛出任何错误,所以我不确定如何解决这个问题。

    internal sealed class Checker {
        private string AssetType(TextAsset fileToBeChecked) {
            string[] line = fileToBeChecked.text.Split (new char[] { '\n' });
            return line [0];
        }
        public bool isItem(TextAsset file) {
            string type = AssetType (file).ToLower ();
            if (type == "helmet" || type == "cuirass" || type == "gauntlets" || type == "gloves" || type == "boots" || type == "leggings" || type == "shield" || type == "robes" || type == "shirt" || type == "pants" || type == "shoes" || type == "hood" || type == "staff" || type == "stave" || type == "sword" || type == "greatsword" || type == "dagger" || type == "mace" || type == "warhammer" || type == "axe" || type == "waraxe" || type == "bow" || type == "misc") {
                return true;
            } else {            
                return false;
            }
        }
    }
}

我的测试文件如下:

Helmet
1
Dragon's Vale
0
10
0
14
1280

调试后

Debug Image

【问题讨论】:

  • 那你调试的时候type是什么?
  • 这应该很容易使用调试器发现。我们不知道AssetType(file).ToLower() 返回什么,因为我们不知道您的上下文。那么我们怎么知道为什么条件永远不会通过呢?
  • @EvanTrimboli 包含信息状态头盔的文件,当我拥有该功能时,在拉动它后给我价值,它说头盔。但是当它与 "helmet" 相比时,它返回 false。
  • 显然不是,否则会匹配。
  • 请只发布重现问题所需的代码。好像 isEnchantment() 和 isSpell() 无关,可以去掉。

标签: c# unity3d


【解决方案1】:

事实证明,我不知道为什么,但是将 Trim() 添加到我的 ToLower() 解决了这个问题,但我没有看到它,因为同一个文件中的另一个错误阻止我正确比较价值观。我已经解决了这两个问题,但我不知道为什么在我的类型字符串之前或之后没有值时 Trim 解决了我的问题。无论如何,脚本都按预期工作。

固定代码是:

private string AssetType(TextAsset fileToBeChecked) {
    string[] line = fileToBeChecked.text.Split (new char[] { '\n' });
    return line [0].Trim ();
}

【讨论】:

  • 如果您添加更改后的行以便其他人可以更快地找到答案,那就太好了,即显示固定代码的位
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-20
  • 2021-11-05
相关资源
最近更新 更多