Search Unity

System.UnauthorizedAccessException - "Access to the path /Unity is denied"

Discussion in 'Scripting' started by Freaking-Pingo, Aug 20, 2016.

  1. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Hi Uniteers, we are encountering an error when building for Android. We are receiving the following error:
    System.UnauthorizedAccessException - "Access to the path /Unity is denied". We are running Unity 5.4.0f3 and are using "Internal only" for write access. We are saving using Application.persistentDataPath. We haven't tested iOS or External (SDCard) for write access. Game runs fine in editor. Additionally, Unity integrated IAP system is having problem saving cache data, because of this error.

    Anyone have an idea? I have never encountered this problem before. My best hunch is that it is a Unity 5.4 error, based on a recent google search find, I found this recent submitted error: http://pastebin.com/vLBFk6U7.
     
    MolangRabbit and MayhemMike like this.
  2. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    No takers? I haven't yet found a solution for this. It appears that if I set write access to SDcard the problem is resolved, however, this is not ideal.
     
  3. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I'll take it back. Setting Write Acces to SDcard produces similar issues. We are pretty stuck at this point.
     
  4. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
  5. arodes

    arodes

    Joined:
    Sep 8, 2015
    Posts:
    5
    We are receiving the same error since Unity 5.4. Seems like a bug. Did you already submitted a bug report?
     
  6. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I submitted a bug report, but I am not sure if I did it properly. I hope they will respond if they find my report inadequate.
     
  7. do2

    do2

    Joined:
    May 30, 2014
    Posts:
    5
    Hi. I'm having the exact same problem as you describe after upgrading to Unity 5.4.1f1. Were you able to find a solution? Or at least were you able to submit a bug report correctly?
     
  8. greenalice

    greenalice

    Joined:
    May 16, 2014
    Posts:
    13
    Same bug here for Unity 5.4.1f1
     
  9. MayhemMike

    MayhemMike

    Joined:
    Oct 23, 2013
    Posts:
    51
    Seems I'm having the same issue but I can hardly advise my users to restart their devices :-/

    Did you guys find a workaround for the bug?
     
  10. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    Alright, as far as I have observed, this appears to only be a problem for certain devices. My old HTC One have not always had this problem, but after some update of Unity I have had this issue. I never found a solution to this, but apparently, I am also one of the only people experiencing this issue.
     
  11. kustom

    kustom

    Joined:
    May 11, 2012
    Posts:
    108
    i have experienced it just today and i cant find a solution... it work si f i set in PlayerPrefs 'Write Permission' to External (SDCard), but i don't want users have to give this kind of permission.

    Update: After reseting my Android device it seems to work again, not a fair solution but enough for me right now
     
    Last edited: Jan 14, 2017
  12. lassade

    lassade

    Joined:
    Jan 27, 2013
    Posts:
    127
    I got this bug in today.

    Here's what happen:
    0. Installed an release version.
    1. Installed an development version of my app
    (this dev version wiped my game data clean)
    2. Switch back to a release version.
    (do not cleared any data)
    4. Got UnauthorizedAccessException.

    I tested this with a blank project. Bug confirmed, at least for me. Using 5.4.1f1, if some one don't mind please test with the newer versions. (The project is attached)

    Any ideas why this happend?
     

    Attached Files:

    Freaking-Pingo likes this.
  13. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    I tried using the example project and your guide, but I couldn't reproduce the bug in 5.5.1f1
     
  14. hjalle

    hjalle

    Joined:
    Mar 15, 2016
    Posts:
    4
    We used to run into this problem on some certain devices, so we are using this workaround, and are now seeing 0 problems related to this (on android).

    private static string GetAndroidFriendlyFilesPath()
    {
    #if UNITY_ANDROID
    AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject applicationContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
    AndroidJavaObject path = applicationContext.Call<AndroidJavaObject>("getFilesDir");
    string filesPath = path.Call<string>("getCanonicalPath");
    return filesPath;
    #endif
    return Application.persistentDataPath;
    }

    private static string GetAndroidFriendlyCachePath()
    {
    #if UNITY_ANDROID
    AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject applicationContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
    AndroidJavaObject path = applicationContext.Call<AndroidJavaObject>("getCacheDir");
    string filesPath = path.Call<string>("getCanonicalPath");
    return filesPath;
    #endif
    return Application.temporaryCachePath;
    }
     
  15. MasterFlu

    MasterFlu

    Joined:
    Jul 12, 2012
    Posts:
    1
    Thank you very much hjalle!!!! Your solution worked like a sharm :)
     
  16. calebbbarton

    calebbbarton

    Joined:
    Mar 20, 2018
    Posts:
    11
    Hi,

    Our project is having the same problem except on IOS devices instead of Android. This is on an Iphone 7+ on the latest OS. Our storage external in Unity. We can save text files fine, however trying to save an image gets denied.

    The image saving is simply converting a texture2D into a byte array and using File.WriteAllBytes to the directory.

    Our .csv files are saving to "Application.persistantDataPath + MyProject/Saves/filename.csv", while our images are saving to "Application.persistantDataPath + MyProject/Images/images.png" (myproject name changed for privacy)

    Thanks
     
  17. bz_tian

    bz_tian

    Joined:
    Jun 28, 2018
    Posts:
    4
    I had the same problem - but I noticed I had created the folder via windows as I thought about the permission system - i can see why it might give an error.. So I went ahead and deleted the folder, then had my program in Unity create it instead.. now there are no problems.. Here's a sample of the working code:

    string s_saveFolder = "Assets/Sales/Saves/";
    try
    {
    if (!Directory.Exists(s_saveFolder))
    {
    Directory.CreateDirectory(s_saveFolder);
    }

    DirectoryInfo dir = new DirectoryInfo(s_saveFolder);
    FileInfo[] info = dir.GetFiles("*.txt");
    foreach (FileInfo f in info)
    {
    Debug.Log("readying in files: " + f.Name);
    }
    }
    catch (IOException ex)
    {
    Debug.Log("IOException ex" + ex.Message + " ... " + ex.StackTrace);
    System.Console.WriteLine(ex.Message);
    }
     
  18. bz_tian

    bz_tian

    Joined:
    Jun 28, 2018
    Posts:
    4
    NOTE -- i DID create the 'Sales' folder off of Assets via the Windows file system (if I remember correctly).. If there's a problem still for yours, perhaps create the folder via the Unity Projects window...
     
  19. shubham_og

    shubham_og

    Joined:
    Jul 20, 2018
    Posts:
    6
    The solution is to Build and Save the apk in the project folder and file name with "MyGame.apk" (That worked for me)
     
    csiszarus likes this.
  20. YousafGrewal

    YousafGrewal

    Joined:
    Jul 5, 2018
    Posts:
    29
    The Same Bug i face in Unity 2018.4.26f1 what the hell with the unity i tried to build apk for last two days but the results are f... i 4 times uninstall SDK and re install and two times unity but the error occur again .The benefit of doing this the nature of error changed know this error occur during i reach on this page now i start downloading new Unity 2019.4 LTS because it seems 2018 is bugy ...
     
  21. sohailthebeast

    sohailthebeast

    Joined:
    Jan 11, 2017
    Posts:
    6
    Did going to 2019 help? I've been recently experiencing this issue as well only on certain devices.
     
  22. T3r0

    T3r0

    Joined:
    Sep 24, 2013
    Posts:
    1
  23. rkachach_

    rkachach_

    Joined:
    Nov 22, 2017
    Posts:
    2
    Thanks to the T3r0 response. It actually solve the issue otherwise you just keep getting the exception even when you are supposed to have write permission.
     
  24. pouria77

    pouria77

    Joined:
    Aug 13, 2014
    Posts:
    36
    Thank you that fixed it for me.
    For anyone wondering, you have to add it to the application tag, like this :

    Code (CSharp):
    1. <application android:requestLegacyExternalStorage="true"
     
    SubstanceShift and Itzhak_P like this.
  25. Itzhak_P

    Itzhak_P

    Joined:
    Apr 28, 2015
    Posts:
    12
    I added the attribute and it didn't work at first. restarted my computer and it worked. thank you.
     
  26. valgrim333

    valgrim333

    Joined:
    Dec 31, 2021
    Posts:
    1
    Deleted the Library directory in Windows Explorer and it went away after Unity rebuilt the directory. I originally built the project on a Google Drive synced folder and moved it so Drive didn't cause problems I think Drive's file syncing screwed it up.
     
  27. mikejm_

    mikejm_

    Joined:
    Oct 9, 2021
    Posts:
    346
    This error just started inexplicably happening for me when I use Application.persistentDataPath. The file path Unity was returning this way was clearly not writable in Android and it would break my function.

    I switched to the above method and now I am again getting a read/writable path.

    Is there something buggy about Unity's method of getting this path that should be fixed by Unity? Why is this happening? For now I will just use this new method, but if this new method is still needed 6 years later, why not make it the core Unity method?

    Or is there at least any explanation for why Application.persistentDataPath would return a usable directory for months on the same device then suddenly not return a writable path while this alternative still does?

    Thanks