- Home /
Unity doesn't Play
I'm trying make a script that turns a string it reads from a .txt file into a usable color variable. When I press the play button unity freeze's and I have to close it and restart it. Here is the script can someone help me out and tell me whats wrong?
import System.IO;
function Start(){
var sr : StreamReader = new StreamReader(Application.dataPath + "/data/playerData.txt");
line = sr.ReadLine();
while(line != null){
if(line.StartsWith("RGBA(")){
var s = line.Substring(5).Split(")"[0]); //i think it has to do with this
var values = s[0].Split(","[0]);
var color : Color = Color(float.Parse(values[0]), float.Parse(values[1]), float.Parse(values[2]), float.Parse(values[3]));
print(color);
}
}
sr.Close();
}
Answer by Meltdown · Aug 10, 2011 at 08:18 PM
The problem you seem to be having is the line...
line = sr.ReadLine();
Should be within your while statement. What you are doing is setting line to the first line, then after that entering a while statement but the value of 'line' is never being updated to the value of the next line, and thus it will never be null, so you will end up with an endless loop, and of course Unity freezing :)
Answer by Eric5h5 · Aug 10, 2011 at 08:16 PM
You made an infinite loop, since line != null, and it will never become null inside the loop, so the loop never ends.