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?

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.

*** " and \r"