Bob Bob - 3 years ago 295
C# Question

File.Exists not working in unity

I'm trying to write a script so the text of a save button changes if it has found a save file in that slot. However, my script cannot seem to find the file no matter what I do. Here's what I have right now.

if (File.Exists ("save1.dat")) {
//set button text
} else {
Debug.Log ("Failure");

I've tried multiple different variations on this. I've tried it with different files, different file extensions, including the Application.dataPath, using Resources.Load, but nothing works. For some reason, these functions cannot seem to find any files in my unity project, despite the fact I can see them clearly in both the unity editor and my file explorer. What are some reasons this might be happening? Are there ways to circumvent this?

Answer Source

The file path you are asking for is not a valid file path. You need to use the Application.dataPath as a root directory and make sure that it ends in a / before appending a file. You may also have to replace \ with / (looking at my own code).

This is sort of a hodgepodge, but I use this to determine the application directory for file IO:

public static class Configuration {
    public static string currentBaseDirectory = "";
    public static string currentDirectory = "";

    public static void loadCurrentDirectory ()
        currentDirectory = Application.dataPath;
        currentDirectory = currentDirectory.Replace( @"\", "/" );
        bool hasFoundMatch = false;

        if ( !currentDirectory.EndsWith( "/" ) )
            currentDirectory += "/";

        switch (Application.platform) {
            case RuntimePlatform.OSXEditor: //<path to project folder>/Assets
            case RuntimePlatform.WindowsEditor:
                if(currentDirectory.EndsWith("Assets/")) {
                    currentDirectory = currentDirectory.Substring(0, currentDirectory.LastIndexOf( "Assets/" ) );
                    currentDirectory += "RuntimeData/";
                    hasFoundMatch = true;
            case RuntimePlatform.WindowsPlayer: //<path to executablename_Data folder>
            case RuntimePlatform.OSXPlayer: //<path to player app bundle>/Contents
                if(currentDirectory.EndsWith(".app/Contents/")) {
                    currentDirectory = currentDirectory.Substring(0, currentDirectory.LastIndexOf( ".app/Contents/" ) );
                    currentDirectory += "RuntimeData/";
                    hasFoundMatch = true;
            case RuntimePlatform.OSXDashboardPlayer: //<path to the dashboard widget bundle>
            case RuntimePlatform.WindowsWebPlayer: //not supported
            case RuntimePlatform.OSXWebPlayer:
                hasFoundMatch = false;

        if (!hasFoundMatch) {
            currentDirectory = Path.GetFullPath("RuntimeData/");
            currentDirectory = currentDirectory.Replace(@"\", "/");

        if (!Directory.Exists( currentDirectory)) {
            for (int i = 0; i < 3; i++)
                currentDirectory = currentDirectory.Substring( 0, currentDirectory.LastIndexOf( "/" ) );
            currentDirectory += "/RuntimeData/";

        currentBaseDirectory = currentDirectory.Replace("/RuntimeData", "");

This allows me to have a RuntimeData directory next to Assets that I can put things like save files in. This folder then ships with the executable when published (although you might want a clean copy, free from any dev testing saves ;) ).

RuntimeData directory location

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download