Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PruneDealer · Oct 06, 2019 at 11:57 PM · savesave databinary

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;
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PruneDealer · Oct 08, 2019 at 03:43 PM 0
Share

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.

avatar image GrayLightGames PruneDealer · Oct 08, 2019 at 10:27 PM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PruneDealer · Oct 08, 2019 at 03:46 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save multiple scriptableObjects in just one binary file 2 Answers

Is there a way to save a 2D characters path and replay it on respawn? 0 Answers

Binary Formatting Loading Error 0 Answers

Best practice: Save informations 0 Answers

Displaying highscore in the main menu scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges