运行游戏后出现这个错误应该怎么解决
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)
GameInfo.ReadInfo () (at Assets/GameInfo.cs:39)
GameInfo.Awake () (at Assets/GameInfo.cs:16)
代码里39行和16行都检查过没发现格式错误
这个意思是转型的时候出错,就是应该是一个数字,但是给了不是一个数字,就转型失败
检查下txt里面是不是有空白行,在末尾
检查数据是否有出错的
可以在报错行输出一下要转型的数据是什么再判断是什么错误
按照老师说的还是没发现错误,下面是源码,报错的地方是在
int id = int.Parse(proArray[0]);这行
public void ReadInfo()
{
string text = objectInfoTexte.text;
string[] strArray = text.Split('\n');
foreach (var str in strArray)
{
string[] proArray = str.Split(',');
ObjectsInfo objectsInfo = new ObjectsInfo();
//获得数据
int id = int.Parse(proArray[0]);
string name = proArray[1];
string icon_name = proArray[2];
string str_type = proArray[3];
ObjectType thisObject = ObjectType.Drag;
switch (str_type)
{
case "Drag":
thisObject = ObjectType.Drag;
break;
case "Equip":
thisObject = ObjectType.Equip;
break;
case "Mat":
thisObject = ObjectType.Mat;
break;
}
//保存数据
objectsInfo.id = id;
objectsInfo.name = name;
objectsInfo.icon_Name = icon_name;
objectsInfo.type = thisObject;
if (thisObject == ObjectType.Drag)
{
//获得数据
int hp = int.Parse(proArray[4]);
int mp = int.Parse(proArray[5]);
int price_sell = int.Parse(proArray[6]);
int price_buy = int.Parse(proArray[7]);
//保存数据
objectsInfo.hp = hp;
objectsInfo.mp = mp;
objectsInfo.price_Sell = price_sell;
objectsInfo.price_Bug = price_buy;
}
//数据保存在字典中
objectsInfoByDict.Add(id,objectsInfo);//Key为id,可以很方便地通过id查找到物品
}
}
这是存储物品属性的txt文件