- Home /
FormatException: Input string was not in the correct format
Hello, I'm trying to read in a series of .txt files from my Resources folder which will generate a tile map. So far, I've been able to parse a single txt file after linking it in the inspector. However, when I try to read in a .txt with Resources.Load, the supposedly same string data cannot be parsed to integers without resulting in the error stated in my the title of this thread.
My code:
var mapList : Object[];
var levelIndex : int;
var numLevels : int;
var mapAsset : TextAsset;
var mapString : String;
function Start () {
mapList = Resources.LoadAll("maps", TextAsset);
levelIndex = 0;
GenerateMap();
}
function GenerateMap () {
// Grab txt from list of maps
mapAsset = mapList[levelIndex];
// Read in level text
mapString = mapAsset.text;
var mapData : Array = mapString.Split(","[0]);
var index : int = 0;
for (var h : int = 0; h<widthInTiles; h++) {
for (var w : int = 0; w<widthInTiles; w++) {
print(mapData[0]);
SpawnTile(w, -h, parseInt(mapData[index])); <<<--- Error
index++;
}
}
}
In summary:
My .txt file is just several lines of numbers separated by commas ("0,0,1,2,3,0" etc.)
parseInt() works fine when I manually reference mapAsset in the Inspector.
If I do print(mapData[0]) where I indicate it prints "0", and printing mapString prints the complete text file as it should.
So.. What the heck is going on?
Answer by DaveA · Nov 20, 2011 at 04:57 AM
Not sure if this will help but try:
var mapData : String[] = mapString......
Also use the debugger and/or Debug.Log to print the elements of mapData to see what they look like.
Yes, you almost never want to use Array. Split returns String[], no point casting that to Array.
Thanks for the quick reply, Dave. Swapping "String[]" for "Array" didn't fix the error, but the error was slightly different. $$anonymous$$aybe it sheds some light on the problem:
"FormatException: Input string was not in the correct format System.Int32.Parse (System.String s)" vs. "FormatException: Input string was not in the correct format int.Parse (string) "
Also, how can I go about using Debug.Log to get more useful info? Sorry still pretty new to program$$anonymous$$g in general.
print (mapData[index]); rather than just 0. Although putting Debug.Log in inner loops can be a problem since it's very slow, so you might want to limit the output in some way. Debug.Log = print.
Thanks, Eric, I should have done that right off the bat. It was actually just a formatting error with one of my .txt files. There were no commas at the end of each line, so what looked like parseInt("00") was actually "0*linebreak*0". And thanks for the tip, Dave.
Your answer
Follow this Question
Related Questions
Help with eval() 0 Answers
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
About "translating" js into C# 1 Answer
FindIndex in a List 1 Answer