Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED]File don't save in persistentDataPath

Discussion in 'Scripting' started by DragOOneD999, May 15, 2019.

  1. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    The txt file refuse to save in persistentDataPath and it get saved in project folder or in game folder if build

    I have the following code:
    Code (CSharp):
    1. if (!Directory.Exists(Application.persistentDataPath + "/Maps"))
    2.             Directory.CreateDirectory(Application.persistentDataPath + "/Maps");
    3.  
    4.         if (!File.Exists(path + "Level01.txt"))
    5.         {
    6.             TextAsset l01 = Resources.Load("Level01") as TextAsset;
    7.  
    8.             string data = l01.text.Replace(Environment.NewLine, string.Empty);
    9.  
    10.             string[] dataArray = data.Split('-');
    11.  
    12.             //File.WriteAllText(path + "Level01.txt", data);
    13.             StreamWriter sr = new StreamWriter(path + "Level01.txt", true);
    14.             for (int i = 0; i < dataArray.Length; i++)
    15.             {
    16.                 if(i == dataArray.Length -1)
    17.                     sr.WriteLine(dataArray[i]);
    18.                 else
    19.                     sr.WriteLine(dataArray[i] + "-");
    20.             }
    21.             sr.Close();
    22.         }
    If I change to dataPath it refuse to create folder.
    Anyone have any idea what is happening ? I use the latest version of unity 2019
     
  2. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    It seems unity dont like
    Code (CSharp):
    1. path = Application.persistentDataPath + "/Maps/";
    I had to change it to
    Code (CSharp):
    1. path = Application.persistentDataPath + "/Maps";
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Use Path.Combine to avoid this kind of issues.
     
  4. DragOOneD999

    DragOOneD999

    Joined:
    Jan 10, 2017
    Posts:
    64
    ok, I will do it. thank you
     
  5. suryavj

    suryavj

    Joined:
    Dec 5, 2018
    Posts:
    2
    how to save image from url to a resource folder using csharp in unity