Home unity3d Save to Unity file

Save to Unity file

Author

Date

Category

In the Unity itself, when I start the game, everything is normal saves, and when compiled in .exe, then does not save anywhere how to fix? To maintain already with a compiled game

using unityengine;
Using System.Collections;
Using System.io;
Public Class Getname: Monobehaviour {
  Public String FileName = "";
  Public Name Char;
  Public String Playername;
  // Use this for initialization
  void start () {
    FileName = "PlayerName.txt";
  }
  // Update IS Called Once Per Frame
  Void Update () {
  }
  void ongui ()
  {
    Name namest = (name) char.getcomponent ("Name");
    Gui.Label (NEW RECT (10, 110, 90, 100), "Your Name:");
    Playername = Gui.TextField (New RECT (90, 110, 100, 20), PlayerName);
    if (GUI.BUTTON (NEW RECT (200, 110, 50, 20), "Save"))
    {
      STREAMWRITER SW = New Streamwriter ("PlayerName.txt");
      SW.WRITELINE (PlayerName);
      SW.Close ();
    }
  }
}

Answer 1, Authority 100%

  1. try to catch an exception, it seems to me that it creates problems you.
    Also, I advise you to use AppDomain to process raw exceptions.
  2. I do not advise you to save the file in this way, specify a more specific path to your file.
  3. Try downloading a file from the code and find out its way, it may be serialized in some other way. For example, in the Temp folder.
  4. also use the class application and its properties PersistentDatapath , streamingassetspath .

Answer 2, Authority 67%

Possible problems while saving:

  • Do you have an exception? If it happens – “catch” it and look for a solution for it.
  • is saved, but another place. Try to specify the full path to the desired directory for the test and make sure whether it is saved to it (prevail to make it a record in this directory).
  • Features of the place of conservation for different platforms. Try to specify also Application.persistentDataPath in the save path.

Answer 3, Authority 33%

Try saving the file elsewhere. For example

streamwriter SW = New Streamwriter (@ "C: /PlayerName.txt"); // for PC
either so for various platforms (Win, MacOS, IOS, Android, etc.)
STREAMWRITER SW = New Streamwriter (Application.persistentDatapath + @ "/ PlayerName.txt");

The last option is optimal and correct if you calculate the various platforms to collect the project.
In general, here.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions