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 /
  • Help Room /
avatar image
0
Question by Batman_1993 · Sep 27, 2015 at 10:15 AM · c#write datatextfile

Adding to a text file. It works but I get this message afterwoods

Hi guys/girls! I'm trying to write to a text file and I keep getting this error message.

 IOException: Sharing violation on path D:\githubStuff\Assets\HighScores.txt
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:320)
 System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share)
 (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
 System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/StreamWriter.cs:124)
 System.IO.StreamWriter..ctor (System.String path)
 (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string)
 GameManager.Awake () (at Assets/Scripts/GameManager.cs:50)

It does write to the file but I'm not sure why this message pops up.

Here is my Code

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 
 public class GameManager : MonoBehaviour {
     public static GameManager instance = null;
 
     private TowerManager tower;
     private DropperCamera camera;
     private CameraFollow playerCamera;
     private GameObject player;
     private GameObject blobbi;
     private GameObject shadow;
     private StreamWriter sw;
 
     // Are we using a controller or KB/mouse?
     public bool controller = false;
 
     void OnLevelWasLoaded(int level) {
         tower = GameObject.Find("Tower").GetComponent<TowerManager>();
         // TODO: DON'T HARDCODE YOUR NAMES NICK YOU MORON
         camera = GameObject.Find("IsoCamera").GetComponent<DropperCamera>();
         playerCamera = GameObject.Find("Main Camera").GetComponent<CameraFollow>();
         player = GameObject.Find("Player");
         blobbi = GameObject.Find("Blobbi");
         shadow = GameObject.Find("ShadowGoo");
         // Ignore physical collisions between the blocks and the world
         // We're doing this manually instead
         Physics.IgnoreLayerCollision(0, 8);
         // Also blocks and other blocks
         Physics.IgnoreLayerCollision(8, 8);
         
     } 
 
     // Use this for initialization
     void Awake () {
         OnLevelWasLoaded (1);
         // Singleton stuff - there can only be one
         if (instance == null)
         {
             instance = this;
         } else if (instance != this) {
             Destroy(instance);    
         }
         
         DontDestroyOnLoad(instance);
         sw = new StreamWriter ("Assets/HighScores.txt");
     }
 
     public GameObject getPlayer()
     {
         return player;
     }
 
     void RestartLevel()
     {    
         string line = (getPlayer ().transform.position.y / 2).ToString ();
         SaveHighScore (line);
         Application.LoadLevel (Application.loadedLevelName);
     }
 
     // Update is called once per frame
     void Update () {
 
 
         if (Input.GetKey (KeyCode.Return))
         {
             RestartLevel();
         }
     }
 
     void SaveHighScore(string line){
         sw.WriteLine (line);
         sw.Flush();
     }
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 Suddoha · Sep 28, 2015 at 12:28 AM

You never properly close/dispose the stream writer, so another script might try to access the file and that usually results in conflicts. I recommend to only open the file when you really need it and dispose it afterwards. There's a nice way to do that (you should do that in the Save method):

 using(StreamWriter sw = new StreamWriter("YourFile"))
 {
       // do the stuff
 }

Using this syntax you won't need to worry about properly disposing. There are still cases that you might need to keep the Stream object for a while in a variable, then you have to fall back to manually Closing/Disposing.

Also, return in the else-part of your Awake method as everything else afterwards will still be executed (Destroy does not destroy immediately, but eventually during the frame, that can be at the very end).

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 Batman_1993 · Sep 29, 2015 at 10:26 AM 0
Share

Thank you!!! Now it works perfectly.

avatar image Suddoha · Sep 29, 2015 at 12:17 PM 0
Share

Just added the missing closing parenthesis. Sorry for that.

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

29 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

Related Questions

Writing to the Externally Mounted SD Card Android 1 Answer

Why is my code not working? [please help] 2 Answers

Reading from file for leaderboard 0 Answers

Help Writing and reading files on android. 1 Answer

Writing Files to Android Device 0 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