Unity中InputField 报错 FormatException: Input string was not in the correct format

参考如下:  https://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format

因为项目需求,需要设置InputFiled的默认值,于是乎就遇到这个报错..在网上查找有不同答案,上述网站就是我所需要的

if (int.Parse(numText.text)>1000)
{
    return;
} 

  之前的代码是这个样子的..

The error means that the string you're trying to parse an integer from doesn't actually contain a valid integer.

It's extremely unlikely that the text boxes will contain a valid integer immediately when the form is created - which is where you're getting the integer values. It would make much more sense to update a and b in the button click events (in the same way that you are in the constructor). Also, check out the Int.TryParse method - it's much easier to use if the string might not actually contain an integer - it doesn't throw an exception so it's easier to recover from.

大神的描述如下..

我们要做的,就是使用它的Int.TryParse方法

更改之后如下:

int result = 0;
if (int.TryParse (numText.text, out result)) {
  if (int.Parse(numText.text)>1000)
  {
      return;
  }
}

  https://www.bilibili.com/video/av11587576  20分50秒左右讲解

posted @ 2018-08-29 17:41  不够自律的人  阅读(5731)  评论(0编辑  收藏  举报