Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BDX777 · Mar 18, 2015 at 01:52 AM · saveloadloadingfilesaving

Sharing Violation On Path

So I've redone my save/load script, but I am encountering a Sharing Violation On Path error.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
 
 public class SaveAndLoad
 {
     public static BinaryFormatter binaryFormatter = new BinaryFormatter();
 
     public static void Save(string saveString, object obj, string saveName)
     {
         string fileType = ".dat";
         string saveGameName = saveName + fileType;
 
         //Create directory if missing
         if(!Directory.Exists("saves/"))
         {
             Directory.CreateDirectory("saves/");
             Debug.Log("Save directory missing, creating new save directory. 'saves/'");
         }
         else
         {
             Debug.Log("Save directory exists.");
         }
 
         //Check if file exists, if not, create file. If the directory does not exist, create one and try again.
         if(File.Exists("saves/" + saveGameName))
         {
             Debug.Log("Save data exists.");
         }
         else
         {
             File.Create("saves/" + saveGameName);
             Debug.Log("Save data missing, creating new empty save data.");
         }
 
 
 
 
 
         //Save game data to file.
         FileStream fileStream = File.Open("saves/" + saveGameName, FileMode.Open);
 
         StreamWriter writer = new StreamWriter("saves/" + saveGameName);
 
         SaveData data = new SaveData();
 
         data.playerStatData.playerAttributes = data.playerStatData.playerAttributes;
         data.playerStatData.playerSkills = data.playerStatData.playerSkills;
 
         writer.Write(data);
 
         //binaryFormatter.Serialize(saveGameName, data.GetData());
         fileStream.Close();
 
 
     }
 
     public static object Load(string saveString, object obj, string saveName)
     {
         string saveGameName = saveName + ".txt";
         string temp = File.ReadAllText(saveGameName);
 
         if(temp == string.Empty)
         {
             return null;
         }
         MemoryStream memStream = new MemoryStream(System.Convert.FromBase64String(temp));
         return binaryFormatter.Deserialize(memStream);
     }
 
     [System.Serializable]
     class SaveData 
     {
         public RPGSystemPlayerStats playerStatData;
 
         public void GetData ()
         {
             playerStatData.ToString();
         }
     }
 }
Comment
Add comment · Show 2
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 BDX777 · Mar 18, 2015 at 04:10 AM 0
Share

I've been rewriting my script a bit, I'll edit this.

avatar image maccabbe · Mar 18, 2015 at 05:14 AM 0
Share

In SaveAndLoad you should probably use the FileStream you open for the StreamWriter or not make a FileStream at all. It seems likely that opening a StreamWriter using the same string as the FileStream is causing your program to access the same file twice which then causes the file sharing violation.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dagra · Mar 18, 2015 at 05:54 AM

I actually learned to do this from digging through the code in this asset so giving credit.

https://www.assetstore.unity3d.com/en/#!/content/20891

I learned a lot from this and its worth getting to dig through the code.

the simplified version of it to get you started is below where load_path and fileContens are all strings.

 using(StreamReader reader = new StreamReader(load_path))
 {
      fileContents = reader.ReadToEnd().ToString();
 }

if you are having trouble getting the proper file path (like I did) use the file picker and debug.log to see your path. load_path = EditorUtility.OpenFilePanel

from there I could parse the string out. hope that helps.

Comment
Add comment · 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

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

21 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

Related Questions

Save works for computer but not once i create the build and load it on the android 1 Answer

Saving and loading game with PlayerPrefs 1 Answer

Problem with saving the transform of the player 1 Answer

loading a downloaded file at runtime 1 Answer

How to prevent loading from freezing the game ? 2 Answers


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