Trying to load high scores... Game is ignoring the defined directory and looking where the .exe is...? Please help

Hi all,

I have a high score ability for my game, whereby it saves and loads .txt files containing the scores. When loading the high scores, it looks at all score txt files in a certain folder, orders them in date order, and then gives a list of Integers the scores and a list of strings the dates. It works brilliantly in the editor. This is the main piece of code that does it:

// load in all scores

    DirectoryInfo info = new DirectoryInfo("C:\\Users\\Nathan\\Desktop\\games\\AvoidTheAsteroids");
    FileInfo[] files = info.GetFiles().OrderBy(p => p.CreationTime).ToArray();
    foreach (FileInfo file in files)
        using (StreamReader sr = new StreamReader(file.ToString()))

A compiled application, however, won’t work. I’m getting the error around the line:

using (StreamReader sr = new StreamReader(file.ToString()))

Can anyone help? It seems the error is to do with DirectoryInfo. According to the output log, it seems to be looking in the directory where the .exe is and not the directory I’ve defined DirectoryInfo “info” as…

Error log states:

IsolatedStorageException: Could not find file “C:\Users\Nathan\Documents\Unity Projects\Avoid the Asteroids\AvoidTheAsteroids03-09-18-39.txt”.

Use relative paths:

   // load 
   string _directoryPath = "Scores";
   if (!Directory.Exists(_directoryPath))
   DirectoryInfo info = new DirectoryInfo(_directoryPath);        
   // your code here..

You also can use PlayerPrefs to store scores for support more platforms.

Debug your game and see what file.ToString() returns.

I suppose that file.ToString() returns the same thing as file.Filename. Since it’s just a filename, Unity thinks you’re giving it a relative path, so it looks relatively to the current working directory (where the .exe is).

You might want to use file.AbsolutePath. I don’t know the exact name of the property, look at the doc if it doesn’t compile.