Reading from file into array

Hello,

It’s my firs post on the forum. I would like to know why Unity freezes when I run the following code. I wanted to make an array with 12 objects, and each object will have 6 properties (a 2 dimension array). After this, I want to read from a file all the properties.

import System.IO;

try{
	// Create an instance of StreamReader to read from a file.
		sr = new StreamReader("TestFile_qlgame.txt");
		// Read and display lines from the file until the end of the file is reached.
		
                line = sr.ReadLine();
		while (line != null) {
					
			var myObj=new Array();    		// the objects
			for(i=0;i<=11;i++){          		// 12 objects
			myObj[i]=new Array();   		// properties of objects
				for(j=0;j<=5;j++){         	// nr of properties for  each object
					myObj[i][j]=sr.ReadLine();
					Debug.Log(myObj[i][j]);    			// DEBUG
				}
			}
		}
		sr.Close();
	}

Thanks in advance.

From what I can see, line isn’t updated, so it can never be null.
That means the while loop will endlessly continue.

Make sure you’ve got a line = sr.ReadLine inside the whilloop aswell, and instead of using myObject_[j] = sr.readline, use myObject*[j] = line.*_
Btw, what are you trying to read ? Have you considered using XML files ?(http://www.unifycommunity.com/wiki/index.php?title=Save_and_Load_from_XML)

It works now. Thanks. I will look over xml too.

You might also have an easier time reading if you use System.IO.File.ReadAllLines as it automatically opens, reads, creates a string array for you of each line in the file, then closes the file.