- Home /
Binary data not finding path
As the total beginner I am, I made the mistake of using playerprefs to save all of my games progress. I changed it to Binary data after watching and following some tutorials (https://www.youtube.com/watch?v=XOjd_qU2Ido) and I am getting a few errors. The main one being "Save file not found in player.UpgSave" the rest I think are caused by this issue. If anyone could tell me what I'm missing it would be greatly appreciated, I can't seem to find anything on google.
Edit: Also, not sure if this has anything to do with it but, in another script I'm referencing (UpgradeData data = SaveUpgrade.LoadUpgrade();) and any of the public ints I am referancing, don't show up, as well as any classes I create such as public void OnExamplePress. Not sure what's up with that either. Sorry about the hassle.
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public static class SaveUpgrade
{
public static void UpgradeSave ( Upgrade upgrade)
{
BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.UpgSave"; //This is the path that is not being found
FileStream stream = new FileStream(path, FileMode.Create);
UpgradeData data = new UpgradeData(upgrade);
formatter.Serialize(stream, data);
stream.Close();
}
public static UpgradeData LoadUpgrade()
{
string path = Application.persistentDataPath + "/player.UpgSave";
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
UpgradeData data = formatter.Deserialize(stream) as UpgradeData;
stream.Close();
return data;
}
else
{
Debug.LogError("Save file not found in " + path); // This is the error
return null;
}
}
}
Answer by GrayLightGames · Oct 07, 2019 at 05:51 AM
Are you calling UpgradeSave before you call UpgradeLoad at least one time? Save is what creates the file, so if you call Load before you save at least once, the file won't be there and it will throw the error you're seeing. You may also want to Debug.Log the filepath to see where the file should be dropping, then go to that location with your file browser to see if it's actually there. If the load and save use the same path, would be good to store it outside Save and Load so if you change the path you only need to do it in one place. Hope that helps, congrats for breaking away from PlayerPrefs :)
Thanks! Super simple yet caused me so many issues. I'll definitely try that as soon as I can, but it definitely makes sense! Thanks again, you've saved me a lot of hours of struggling.
Glad I could help, hopefully that fixes it! Getsumi also made a good point in the other comment, it would be good to have your load function create or process default data if it doesn't find a file. You can also call the save function after loading the default data and then that code path will only be reached on the initial load. Good luck!
Answer by Getsumi3 · Oct 07, 2019 at 06:40 AM
First of all I'd suggest to use Path.Combine()
to make sure that the path to your file will always be valid.
When saving in UpgaredSave() do a check if the player.UpgSave exists before doing any other actions. If it doesn't than create your player.UpgSave and do the rest of your actions.
Same for UpgaredLoad(). Before laoding check if the player.UpgSave exists. If it doesn't than LoadDefaultData(), else load saved data.
I will definitely try this because I've seen other people suggest to add Path.Combine(). I put it in my code at one point but never got to test it because of the errors I was getting. Thanks for the input!
Your answer
![](https://koobas.hobune.stream/wayback/20220612213102im_/https://answers.unity.com/themes/thub/images/avi.jpg)