Search Unity

Question Unity Freeze when reading files

Discussion in 'Scripting' started by unity_xXyJqtEjf2yZLQ, Dec 5, 2022.

  1. unity_xXyJqtEjf2yZLQ

    unity_xXyJqtEjf2yZLQ

    Joined:
    Feb 7, 2022
    Posts:
    5
    So, I was trying to read score and name's initial from a text files, but whenever I pressed play in Unity, the program just freezes. While I believe I got an infinite loop, I still don't know how did I got there.

    Here is the code and the only loop:
    Code (CSharp):
    1. highscoreEntryList = new List<HighScoreEntry>();
    2.  
    3.         try
    4.         {
    5.             sr = new StreamReader(Application.dataPath + "/" + "High Score.txt");
    6.             string dataline = "";
    7.             dataline = sr.ReadLine();
    8.             while (dataline != "")
    9.             {
    10.                 Debug.Log(dataline);
    11.                 string[] value = dataline.Split(",");
    12.                 int score1 = int.Parse(value[1]);
    13.                 highscoreEntryList.Add(new HighScoreEntry { score = score1, name = value[0] });
    14.             }
    15.             sr.Close();
    16.         }
    17.         catch (IOException e)
    18.         {
    19.             Debug.Log(e.Message);
    20.         }
    Here's the file that I'm trying to read:
    Code (CSharp):
    1. HN,0
    2. NH,0
    3. KH,0
    4. MI,0
    5. JK,0
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You never read the next line. Your ReadLine is outside your while loop, so once you enter your while loop, you're stuck there forever as dataline never changes value.
     
    Bunny83, Joe-Censored and Nad_B like this.
  3. unity_xXyJqtEjf2yZLQ

    unity_xXyJqtEjf2yZLQ

    Joined:
    Feb 7, 2022
    Posts:
    5
    Thank you. Now that you've said it, it sounds so obvious, but it took me hours to eventually asking for help lol
     
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    723
    Also don't do
    while (dataline != "")
    , but
    while (dataline != null)
    , since .NET's
    StreamReader
    will return a
    null
    at the end of the file, not an empty string. And you should always* wrap your
    IDisposable
    s like
    StreamReader
    s into a
    using
    block to make sure they're disposed properly, even if your code throws an exception.

    Here's the working code for future readers:
    Code (CSharp):
    1. // Always use a "using" statement instead of manually calling .Close()
    2. // to make sure your Stream is closed even if your code throws an exception
    3. using (var sr = new StreamReader(@"D:\your-text-file.txt"))
    4. {
    5.     string dataline;
    6.     while ((dataline = sr.ReadLine()) != null)
    7.     {
    8.         Debug.Log(dataline);
    9.     }
    10. }
    *exceptions being:
    MemoryStream
    (not needed, as it's just a wrapper around a byte array anyway), and .NET's
    HttpClient
    (to avoid Socket exhaustion + better performance)
     
    Last edited: Dec 5, 2022
    Bunny83 and jvo3dc like this.