public void LoadControlls(KeyCode[] key){
string[] FilePaths = Directory.GetFiles(Application.dataPath + "/Save/Controlls/", "*.sav", SearchOption.AllDirectories);
if (FilePaths == null){
Debug.Log ("works");
}
}
if I put it this way it Looks like it works but when I change it to:
if (FilePaths[0] == null){
I get error witch is logical
IndexOutOfRangeException: Array index is out of range.
SaveLoadS.LoadControlls (UnityEngine.KeyCode[] key) (at Assets/Scripts/Class/Struct/SaveLoadS.cs:28)
MainMenu.Start () (at Assets/Scripts/MainMenu.cs:36)
how do I check if no files exist
for testing purposes directory Controlls MUST be empty
Instead of testing if the first index of the FIlePaths array is null, you can attempt to test if any files were returned by doing something like this:
if (FilePaths != null)
{
if (FilePaths.Length > 0)
{
Debug.Log("Files were found!");
}
else
(
Debug.Log("No files were found!");
)
}
else
(
Debug.Log("No array was returned!");
)
That is, check the count of the array instead. If it is less than zero or zero it means no files exist.
You can’t simply access the zero index of an array if the count is 0. You get an index out of bounds error, which means that you are accessing an array with no elements in it. Check for the length of the array first.