- Home /
Index is Less Than Zero + Flawed Code
Can you tell me what is wrong with my code? Essentially, I am trying to loop through a tab seperated file which I load and then create prefabs when it reaches a certain character. I have not gotten to the prefab code yet, as I am simply trying to properly loop through this multidimensional array.
The code is here:
import System.IO;
var ddata_b = new Array();
var ddata = new Array();
function Start () {
try {
var sr = new File.OpenText("Assets\\dungeon.txt");
var input = "";
var i = 0 ;
while (true) {
input = sr.ReadLine();
if (input == null) { break; }
ddata_b[i] = input;
i++;
}
sr.Close();
} catch (e) {
// Let the user know what went wrong.
print("The file could not be read:");
print(e.Message);
}
//done grabbing data
var i2 = 0;
while(ddata_b.length >= i2) {
ddata[i2] = ddata_b[i2].ToString().Split(" "[0]);
i2++;
}
}
populate(ddata);
function Update () {
}
function populate(inp) {
var x=0;
var z=0;
while(inp.length >= x) {
Debug.Log("This is being run...");
while(inp[x].length >= z) {
if(inp[x][z]=="F") Debug.Log("Floor tile at: " + x + z);
z++;
}
z=0;
x++;
Debug.Log("New Line.");
}
Debug.Log("Done.");
}
I am getting the ArgumentOutOfRangeExceptions (Index is Less Than Zero or more than or equal to the lsit count) errors at these lines:
while(inp[x].length >= z) {
and
ddata[i2] = ddata_b[i2].ToString().Split(" "[0]);
Thanks guys!
Answer by jchow · Feb 04, 2014 at 02:50 AM
Don't you want
while(inp.length > x) and while(inp[x].length > z)
?
Your answer
Follow this Question
Related Questions
IndexOutofRangeException 1 Answer
Array index is out of range 0 Answers
IndexOutOfRangeException error for array points in range 1 Answer
Problems with Arrays 2 Answers
XML File, or Array? 1 Answer