-2
if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))

This line always causes a null reference exception instead of returning false when it cannot find the file.

  • Actually no it doesn't, the code within gives the null reference exception, please debug your code to work out why – TheGeneral Mar 29 '19 at 01:35
  • Extract `Application.persistentDataPath` and `Input.GetComponent()` out of the call and check values for both in the debugger. – J.R. Mar 29 '19 at 01:40
  • When I clicked on the error message it gave the error for this line of code, and none of the other code even triggers without going through this if statement – nate300and4 Mar 29 '19 at 01:40
  • once again, `File.Exists` is **not** the problem, the code inside it is – TheGeneral Mar 29 '19 at 01:43
  • A `NullReferenceException` happens when you try to access a member (property, method, field) of an object that's `null`. Therefore, one of these things is `null` => `Application`, `Input`, or `GetComponent()` – Rufus L Mar 29 '19 at 01:45

1 Answers1

0

File.Exists is not problem. Think about fixing your code like this

    if (Application.persistentDataPath != null && Input != null && Input.GetComponent<Text>() != null && Input.GetComponent<Text>().text != null)
    {
        if(File.Exists(Application.persistentDataPath + "/users/" + Input.GetComponent<Text>().text + ".dat"))
Bùi Đức Khánh
  • 2,435
  • 4
  • 24
  • 38