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 blackstarmaster11 · Mar 18, 2020 at 10:00 AM · save dataload scene

Sharing violation on path... Save Game Issue.

Hello, I am trying to make a save game system for my game but this error is making problems for it.

Here is the code: (SaveSystem.cs)

 using UnityEngine;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 
 public static class SaveSystem
 {
     public static void SaveProgress (StartGame level)
     {
         BinaryFormatter formatter = new BinaryFormatter();
 
         string path = Application.persistentDataPath + "/game.dat";
         FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
 
         PlayerData data = new PlayerData(level);
 
         formatter.Serialize(stream, data);
         stream.Close();
     }
 
     public static PlayerData LoadProgress()
     {
         string path = Application.persistentDataPath + "/game.dat";
 
         if(File.Exists(path))
         {
             BinaryFormatter formatter = new BinaryFormatter();
             FileStream stream = new FileStream(path, FileMode.Open);
 
             PlayerData data = formatter.Deserialize(stream) as PlayerData;
             stream.Close();
 
             return data;
         }
         else
         {
             Debug.Log("Save file not found.");
             return null;
         }
     }
 }
 

PlayerData.cs

 using UnityEngine;
 
 [System.Serializable]
 public class PlayerData : MonoBehaviour
 {
     public int GameProgress;
 
     public PlayerData(StartGame level)
     {
         GameProgress = level.GameProgress;
     }
 }
 

And at last: (StartGame.cs)

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class StartGame : MonoBehaviour
 {
     public int GameProgress = 0;
     public void StartGamePlay()
     {
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 4);
     }
 
     public void BackToMenu()
     {
         SceneManager.LoadScene(1);
     }
 
     public void LoadLevelScene()
     {
         SceneManager.LoadScene(2);
     }
 
     public void LoadOptionsScene()
     {
         SceneManager.LoadScene(3);
     }
 
     private void Update()
     {
         int CurrentLevel = SceneManager.GetActiveScene().buildIndex;
 
         if(CurrentLevel == 6)
         {
             GameProgress = 1;
             Debug.Log(GameProgress + "       " + CurrentLevel);
             SaveGameProgress();
         }
     }
 
 
     public void SaveGameProgress()
     {
         SaveSystem.SaveProgress(this);
     }
 
     public void LoadGameProgress()
     {
         PlayerData Data = SaveSystem.LoadProgress();
 
         GameProgress = Data.GameProgress;
     }
     public void ContinueGame()
     {
         LoadGameProgress();
         if(GameProgress == 1)
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 5);
         }
         else if (GameProgress == 2)
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 6);
         }
         else
         {
             Debug.Log("Game Progress is Null");
         }
     }
 }
 

The error I am getting when I am on scene 6 is "Sharing violation on path ..."Directory" ".

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

1 Reply

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

Answer by ShadyProductions · Mar 18, 2020 at 10:07 AM

This error usually happens when you are trying to open a stream to a file, that was already opened in a previous stream and not closed yet.

So in other words, the file you are trying to read/write to has a lock.

Comment
Add comment · Show 4 · 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 blackstarmaster11 · Mar 18, 2020 at 10:18 AM 0
Share

I tried using "OpenOrCreate" and other solutions but nothing worked. Also tried changing the extension, name and even change the script... yet the same problem...

avatar image ShadyProductions blackstarmaster11 · Mar 18, 2020 at 10:27 AM 0
Share

In scene 6 you are saving your game every frame, why?

  private void Update()
  {
      int CurrentLevel = Scene$$anonymous$$anager.GetActiveScene().buildIndex;
 
      if(CurrentLevel == 6)
      {
          GameProgress = 1;
          Debug.Log(GameProgress + "       " + CurrentLevel);
          SaveGameProgress();
      }
  }

This is probably the problem, the file cannot close fast enough before the next frame it is read again.

avatar image blackstarmaster11 ShadyProductions · Mar 18, 2020 at 01:06 PM 0
Share

Yes, that was the problem I guess, changed it to void start() and I get another type of error, I'll let you know in a few $$anonymous$$utes if that solved my problem totally.

Thanks though.

Show more comments

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

124 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

password-based checkpoints 0 Answers

Which is the correct way to save character preferences like clothes? 1 Answer

Save and Load Instantiate Enemies 2 Answers

Chest contents persistence between scenes 1 Answer

Create Level save files from runtime Level Edits 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