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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM