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秒左右讲解

转载于:https://www.cnblogs.com/bw1219/p/9555863.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值