Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script works in editor but not XAML build

Discussion in 'Editor & General Support' started by AirtopGames, May 17, 2022.

  1. AirtopGames

    AirtopGames

    Joined:
    Mar 24, 2022
    Posts:
    2
    I have a project that includes a coin system that is saved to a path. Every time the game is loaded, the coins will try to load into a TMPro object. Here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5. using System;
    6. using System.IO;
    7. using System.Reflection;
    8.  
    9. public class ScriptManager : MonoBehaviour
    10. {
    11.     public TextMeshProUGUI tmp;
    12.     string coins;
    13.     int cc;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\gameccint.gamec"))
    19.         {
    20.             coins = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\gameccint.gamec");
    21.             tmp.text = coins.ToString();
    22.             UnityEngine.Debug.Log("Loaded coins: " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
    23.         }
    24.         else
    25.         {
    26.             cc = 0;
    27.             File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\gameccint.gamec", cc.ToString());
    28.             coins = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\gameccint.gamec");
    29.             tmp.text = coins.ToString();
    30.             UnityEngine.Debug.Log("Loaded coins: " + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
    31.         }
    32.     }
    33. }
    But when I build the project in Debug mode, it gives me these errors in the development console:
    Code (CSharp):
    1. DirectoryNotFoundException: Could not find a part of the path "C:\Users\Admin\Documents\gameccint.gamec"
    2.  
    3. DirectoryNotFoundException: Could not find a part of the path "C:\Users\Admin\Documents\gamecgint.gamec"
    4.  
    5. FormatException: Input string was not in a correct format
    I can't seem to figure out why it works perfectly fine in the editor but not in the build.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
  3. timke

    timke

    Unity Technologies

    Joined:
    Nov 30, 2017
    Posts:
    395
    Correct, UWP heavily restricts the directories an app can access. See the Microsoft documentation for UWP file access rules. The Application.persistantDataPath points to:
    %userprofile%\AppData\Local\Packages\<productname>\LocalState
    which your UWP app is allowed to read/write to.
     
  4. AirtopGames

    AirtopGames

    Joined:
    Mar 24, 2022
    Posts:
    2
    So all I need to do is to replace:
    Code (CSharp):
    1. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    with this?
    Code (CSharp):
    1. Application.persistentDataPath
     
  5. timke

    timke

    Unity Technologies

    Joined:
    Nov 30, 2017
    Posts:
    395
    That would be my recommendation because it should "just work".

    Now, UWP apps can access the Documents folder but it requires configuring your app's Capabilities (permissions), which is something the end-user can approve or revoke (just like on Android or iOS).

    Here's a quick walkthrough on how to do this through the Visual Studio project generated by Unity (when you Build for UWP). I don't know offhand if you can make this work with Build & Run.

    In general I recommend avoiding Documents, Pictures, and other "special" folders for storing game data.