Save game in editor works but not in real game

Hi All
I save game data to a file. this works fine in the editor. But when I try to save in a build it doesn’t.
My path is /home/user/Dropbox/Unity3d/Projects/7.GameSystem/Assets/Resources/Games/Game-Ver2.00/BookKeeping

        gamePath = "Games/";
        gamePath += fileHandler.GameName;
        gamePath += "-";
        gamePath += fileHandler.VersionName;
        gamePath += "/BookKeeping/LastGame";

Do I need to add something else to the path for a real build
Thanks

Is there anything else you use before gamePath? Something like Application.dataPath?

The location of the game is not going to be the same for all users, so the moment the game encounters a folder in the path that doesn’t exist, it stops and complains it cant find it.

Hard to say what the problem truly is without seeing how you have made use of gamePath.

1 Like

Use Application.persistentDataPath for saving files at runtime. I don’t believe saving to the Resources folder at runtime is supported either way. And you shouldn’t be using an absolute path (I don’t have the /home/user/Dropbox/Unity3d/Projects/… folder on my pc).

1 Like

Hi I’ve made my game path exactly as the code?

I tried adding

           gamePath = Application.dataPath + "/Resources/";
        gamePath += "Games/";
        gamePath += tcp.fileHandler.GameName;
        gamePath += "-";
        gamePath += tcp.fileHandler.VersionName;
        gamePath += "/BookKeeping/LastGame";

But this errors on opening the file " if( !file.openFile( gamePath ) )" with, it seems to add the path by it’s self?

DirectoryNotFoundException: Could not find a part of the path “/home/user/Dropbox/Unity3d/Projects/7.GameSystem/Assets/Resources/home/user/Dropbox/Unity3d/Projects/7.GameSystem/Assets/Resources/Games/Game-Ver2.00/BookKeeping/LastGame”.
System.IO.FileStream…ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:292)
System.IO.FileStream…ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int)
System.IO.File.Create (System.String path, Int32 bufferSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:135)
System.IO.File.Create (System.String path) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:130)
FileControl.openFile (System.String game_path) (at Assets/Scripts/Utilities/FileControl.cs:55)
LastGame_Bookeeping.Start () (at Assets/Scripts/BookKeepting/LastGame_Bookeeping.cs:130)

Thanks started to use “Application.persistentDataPath” however I think it might another issue, the player.log is reporting and issue with the use of the data, I think it might be an issue with script order, running different in real game to editor?

Can’t help you if you don’t explain your issue mate, and a hypothesis about the problem is of no help either.

1 Like

To add to that, there is a difference between using dataPath and persistentDataPath. It depends entirely on where you want to save your data. persistentDataPath saves in the same default location as playerprefs, while dataPath is the location of the game assets folder.

1 Like

Thanks all, the persistentDataPath worked. There was a slight other issue, but that was the main one.
My other problem was the data being read in gave an issue because of script start up. I was basically calling something that didn’t exist yet. So I set the script order which fixed it. Fairly new to Unity so didn’t realise the final build might put scripts in different start up order.

While it’s true, you can alter the order of your scripts, I think it’s far, far, far more common (better?) to organize your scripts using the execution order of methods.
If you need to load/set something up earlier, use Awake(), and then any later scripts will try to access that information in Start().

persistentDataPath was the right choice.

PlayerPrefs and persistentDataPath are not the same.

1 Like