When I Build my game it cannot load a JSON file.
Hi Guys,
I am about to make a 2d platformer game and am finished the highscore saving method. In the editor it can make a highscore.json and fill with the initials, and then Load it. After i build that game it created the highscore.json file but not fill with any data. And when i want to add a row in this file it can not work at all.
     public static List Rankings;
     private static string FILENAME = "highscores.json";
     private static int MaxScores = 5;
 
 
     static GlobalScoreHandler()
     {
         Rankings = new List<HighScore>();
     }
 
 
     public static void Load()
     {
         if (!File.Exists(FILENAME))
             CreateInitial();
         StreamReader sr = new StreamReader(FILENAME);
         List<string> scores = new List<string>();
         while (!sr.EndOfStream)
         {
             scores.Add(sr.ReadLine());
         }
         sr.Close();
 
         Rankings = new List<HighScore>();
         foreach(string s in scores)
         {
             Rankings.Add(HighScore.FromJson(s));
         }
     }
 
     public static void Save()
     {
         if (File.Exists(FILENAME))
             File.Delete(FILENAME);
         StreamWriter sw = new StreamWriter(FILENAME);
         foreach (var item in Rankings)
         {
             sw.Write(item.ToJson());
             sw.Write("\n");
         }
 
         sw.Close();
     }
 
     public static void AddScore(HighScore s)
     {
         if (Rankings[Rankings.Count - 1].Score <= s.Score)
         {
             Debug.Log("HighScore is not enough to be listed.");
             return;
         }
 
         Rankings.ForEach(y =>
         {
             if(y.FinalScore == s.FinalScore && s.Name == y.Name)
                 return;
         });
 
         Rankings.Add(s);
         
         Rankings.Sort();
 
         Rankings.RemoveAt(MaxScores - 1);
 
         Save();
 
     }
 
 
     public static void CreateInitial()
     {
         Debug.Log("Initialize started... Creatin default...");
         DifficultyScript.CurrentDifficulty = DifficultyScript.Difficulty.HARD;
         HighScore hs = new HighScore().WithName("Dev_1").WithScore(1020).WithDiff("HARD");
         Rankings.Add(hs);
         DifficultyScript.CurrentDifficulty = DifficultyScript.Difficulty.MEDIUM;
         hs = new HighScore().WithScore(1010).WithName("Dev_2").WithDiff("MEDIUM");
         Rankings.Add(hs);
         DifficultyScript.CurrentDifficulty = DifficultyScript.Difficulty.EASY;
         hs = new HighScore().WithScore(1200).WithName("Dev_3").WithDiff("EASY");
         Rankings.Add(hs);
         DifficultyScript.CurrentDifficulty = DifficultyScript.Difficulty.EASY;
         hs = new HighScore().WithScore(1300).WithName("Dev_4").WithDiff("EASY");
         Rankings.Add(hs);
         DifficultyScript.CurrentDifficulty = DifficultyScript.Difficulty.EASY;
         hs = new HighScore().WithScore(1500).WithName("Dev_5").WithDiff("EASY");
         Rankings.Add(hs);
 
         DifficultyScript.CurrentDifficulty = DifficultyScript.Difficulty.NONE;
 
         Save();
 
     }
 
     public static void CheckHighscoreList()
     {
         if (!File.Exists(FILENAME))
             CreateInitial();
         else
             Load();
 
         Debug.Log("Done loading & saving default scores");
     }
 
 
               Here is a script where i create and fill the json file. In the editor it works, but when i build it it absolutly not works.
And that
public void SaveHighscore() {
     HighScore hs = new HighScore().WithScore((Mathf.Round((float)GameMaster.Timer))).WithName(NameStore.TName).WithDiff(DifficultyScript.GetStringifiedValue());
     GlobalScoreHandler.AddScore(hs);
     GlobalScoreHandler.Save();
     SceneManager.LoadScene("highscores");
 }
 
               When i want to add a row in this json file. In editor it works perfectly and sort it. But when i build it itt I can add a row and also it cannot open the highscores scene too i dontknow why.
Please somebody help me. I dont know what to do now
Also, your code looks mostly fine, but i can see issues, Eg you'll want a try-catch in the read/save method as the file might be in use OR the user running the application might not have permission to read/write from the folder the application is in resulting in an Access Denied error. I'd suggest using somthing like Application.persistentDataFolder (can't remember exact name)
also... if you want to read/write a list of strings to a file you can do somthing like File.WriteAllLines and File.ReadAllLines.
It can be cleaner code and reduce risk of locking resources. And for a stream reader/writer you'd really want that in a using() statement if it can be disposed as you might be locking the file if the read/write fails before it closes. using() auto disposes if there is an error or not as the compiler turns it into a try-finally
Your answer
 
             Follow this Question
Related Questions
error in c# for creating list of quiz data 2 Answers
2d platformer flipping problem 1 Answer
Unity for each JSONnode in JSONObject 0 Answers
2D c# player Controller issues,2D c# player Controller issue 0 Answers
Changing Color 1 Answer