Debug
Debug

Reputation: 1

Unity get_dataPath not allowed

I have a problem use Application.dataPath

The Error: UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'SaveAndLoadMenu' on game object 'Save And Load Menu'. See "Script Serialization" page in the Unity Manual for further details. FilePaths..cctor () (at Assets/_MAIN/Scripts/Core/IO/FilePaths.cs:8) Rethrow as TypeInitializationException: The type initializer for 'FilePaths' threw an exception.

The Error indicate FilePaths.cs at public static readonly string root = $"{Application.dataPath}/gameData/"; this line

And Menupage.cs inherited MonoBehaviour

public class FilePaths
{
    private const string HOME_DIRECTORY_SYMBOL = "~/";

    
*    public static readonly string root = $"{Application.dataPath}/gameData/";* // Error 


    //Runtime Path
    public static readonly string gameSaves = $"{runtimePath}Save Files/";


    public static readonly string resources_font = "Fonts/";
    public static readonly string resources_graphics = "Graphics/";
    public static readonly string resources_backgroundImages = $"{resources_graphics}BG/";
    public static readonly string resources_backgroundVideos = $"{resources_graphics}BG Videos/";
    public static readonly string resources_blendTextures = $"{resources_graphics}Transition Effects/";

    public static readonly string resources_audio = "Audio/";
    public static readonly string resources_sfx = $"{resources_audio}SFX/";
    public static readonly string resources_voices = $"{resources_audio}Voices/";
    public static readonly string resources_bgm = $"{resources_audio}BGM/";
    public static readonly string resources_ambience = $"{resources_audio}Ambience/";

    public static readonly string resources_dialogueFiles = $"Dialogue Files/";

    public static string GetPathToResource(string defaultPath, string resourceName)
    {
        if(resourceName.StartsWith(HOME_DIRECTORY_SYMBOL))
        {
            return resourceName.Substring(HOME_DIRECTORY_SYMBOL.Length);
        }

        return defaultPath + resourceName;
    }


    public static string runtimePath
    {
        get
        {
            #if UNITY_EDITOR
                return "Assets/appdata/";
            #else
                return Application.persistentDataPath + "/appdata/";
            #endif
        }
    }
}
public class SaveAndLoadMenu : MenuPage
{
    public static SaveAndLoadMenu Instance {get; private set;}

    const int MAX_FILES = 99;
    string savePath = FilePaths.gameSaves;
    int currentPage = 1;
    bool loadedFilesForFirstTime = false;

    public enum MenuFunction {save, load}
    public MenuFunction menuFunction = MenuFunction.save;

    public SaveLoadSlot[] saveSlots;
    int slotsPerPage => saveSlots.Length;

    public Texture emptyFileImage;

    void Awake()
    {
        Instance = this;
    }

    public override void Open()
    {
        base.Open();

        if(!loadedFilesForFirstTime)
            PopulateSaveSlotsForPage(currentPage);
    }

    void PopulateSaveSlotsForPage(int pageNumber)
    {
        int startingFile = ((currentPage - 1) * slotsPerPage) + 1;
        int endingFile = startingFile + slotsPerPage -1;

        for(int i = 0; i < slotsPerPage; i++)
        {
            int fileNum = startingFile + i;
            SaveLoadSlot slot = saveSlots[i];

            if(fileNum < MAX_FILES)
            {
                slot.root.SetActive(true);
                string filePath = $"{FilePaths.gameSaves}{fileNum}{VNGameSave.FILE_TYPE}";
                slot.fileNumber = fileNum;
                slot.filePath = filePath;
                slot.PopulateDetails(menuFunction);
            }
            else
            {
                slot.root.SetActive(false);
            }
        }
    }
}

I was tried

public static string Root { get; private set; }

private void Awake()
{
    Root = $"{Application.dataPath}/gameData/";
}

Upvotes: 0

Views: 301

Answers (1)

derHugo
derHugo

Reputation: 90724

As the error is telling you you can not use Application.dataPath at field initialization

public static readonly string root = $"{Application.dataPath}/gameData/";

what you can do though is e.g. using

public static string root { get; private set ;}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void Init()
{
    root = $"{Application.dataPath}/gameData/";
}

See [RuntimeInitializeOnLoadMethod]

Upvotes: 1

Related Questions