Sunday, July 24, 2016

Isolatedstorageexception : Could Not Find File

Isolatedstorageexception : Could Not Find File

I'm trying to save/load player data from a file using the persistenceDataPath using the following code:

private void Save()
    {
        if (File.Exists (Application.persistentDataPath + "/playerInfo.dat"))
        {
            FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
            data.userExperience = userExperience;
            bf.Serialize (file, data);
            file.Close ();
        }
    }

private void Load() 
{   
    if (File.Exists (Application.persistentDataPath + "/playerInfo.dat"))
    {   
        FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat ", FileMode.Open);
        PlayerData data = (PlayerData)bf.Deserialize (file);
        userExperience = data.userExperience;
        file.Close ();
    }
}

[Serializable]
class PlayerData
{
    public int userExperience;
}

It works fine if I play the game with Unity Editor. However, on Android, when I try to execute this line of code in Load():

FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat ", FileMode.Open);

I get this error: 

 

UdjCG.png

 

 

The file does exist since the code pass through the if(File.Exist...) and it works when I try to access the file from the Save() method.

I really don't know what it could be since the code that open the file in Save() and Load() is the same.


No comments:

Post a Comment