- Home /
File exist, path is null but isn't C#
Hi, I'm trying to load file as text. This is working in editor but not in build:
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open("Assets/Door/Resources/Saves" + "/myGame_" + saveLevelInt + "_" + saveSlotInt + ".txt", FileMode.Open);
playerData data = (playerData)bf.Deserialize(file);
file.Close ();
// do rest
Because it's not working in build I tried to use Resources.Load but it looks like I miss something, here is code:
BinaryFormatter bf = new BinaryFormatter();
TextAsset hila = (TextAsset)Resources.Load("Saves/myGame_" + saveLevelInt + "_" + saveSlotInt, typeof(TextAsset));
if(hila == null)
print("there is no file");// never prints if not modify path and file exist in folders
FileStream file = File.Open(hila.ToString(), FileMode.Open); // in this line i get error**
playerData data = (playerData)bf.Deserialize(file);
file.Close ();
//do rest
**that error:
ArgumentException: Path is empty.
Problem is I cannot make saves in runtime, they are always from files made before game is builded. It's specyfic case :D
@$$anonymous$$amil1064 You need this one :P
http://answers.unity3d.com/questions/279750/loading-data-from-a-txt-file-c.html
Hello,
Not sure that Hila.ToString() will actually work, maybe try hila.Text (doc)
ANd use if(String.IsNullOrEmpty(hila.Text)) print("...");
Hi @Nerevar, now it prints always :( Trying now to understand links which @saud_ahmed020 gave. Edit: $$anonymous$$odified line to:
TextAsset hila = (TextAsset)Resources.Load("Saves/myGame_" + saveLevelInt + "_" + saveSlotInt + ".txt", typeof(TextAsset));
if(string.IsNullOrEmpty(hila.text))
print("something wrong"); // now never prints if file exist in folder but get bug:
//NullReferenceException: Object reference not set to an instance of an object// in line above
// "if(string.IsNullOrEmpty(hila.text))"
I made hila as public TextAsset, then trying to File.Open... but then path is null as on first post :(
@$$anonymous$$amil1064 try this http://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
@saud_ahmed020 I tried but it's missing when i move application between computers :(