- Home /
Loading a saved Game
I have a script that saves certain variables to an .xml and then recalls them on load. When some plays for the first time they do not have this .xml file. If the player attempts to press the load button it throws an error saying it has not found the file. Once the player presses the save button then the game functions fine. I want to set an if statement to check if the file exists and if not then tell the player to save a file.The error is here:
FileNotFoundException: Could not find file "/Users/ParkerCain/Desktop/Lucky Leprachaun/Assets/SaveData.xml".
all i need to do is check for it and if not there return null or something and give instruction. I don't know how to check for that however.
Here is my code:
function LoadXML()
{
var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
var _info : String = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log("File Read");
}
My assumption is that it should be something along the lines of :
function LoadXML()
{
if( //file is found)
{
var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
var _info : String = r.ReadToEnd();
r.Close();
_data=_info;
Debug.Log("File Read");
}
else()
{
Debug.Log("File not found");
}
}
Thanks for the help everyone.
Answer by syclamoth · Nov 30, 2011 at 07:04 AM
Check out the MSDN documentation for the 'File' class! One of the things in there is the function File.Exists(string)- Which is not only exactly what you need here, but also kind of obvious, when you think about it.
if(File.Exists(_FileLocation+"/"+ _FileName))
{
// Do the rest of the function!
}
Your answer
Follow this Question
Related Questions
Sharing Violation On Path 1 Answer
C# PlayerPrefs not Work 2 Answers
Best way to save data? (rpg game) 0 Answers
How do I save a list of scriptable objects? 0 Answers
Save Player Health and stats 1 Answer