0

I'm getting a null reference error when testing the login and register functions in the Unity app I'm building. Both errors are virtually the same line of code, so I'm guessing that something isn't connecting with Firebase correctly.

I'm using Unity 2021.1.12f1

Here is the relevant code (didn't want to post too much, hoping that this is enough to help diagnose the problem.

public class FirebaseManager : MonoBehaviour
{
    public static FirebaseManager instance;

    public FirebaseAuth auth;
    public FirebaseUser user;

    FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(checkDependencyTask =>
        {
            var dependencyStatus = checkDependencyTask.Result;

            if (dependencyStatus == DependencyStatus.Available)
            {
                InitializeFirebase();
            }

private void InitializeFirebase()
    {
        auth = FirebaseAuth.DefaultInstance;

        auth.StateChanged += AuthStateChanged;
        AuthStateChanged(this, null);
    }

public void RegisterButton()
    {
        StartCoroutine(RegisterLogic(registerUsername.text, registerEmail.text, registerPassword.text, registerConfirmPassword.text));
    }

private IEnumerator RegisterLogic(string _username, string _email, string _password, string _confirmPassword)
    {
        if (_username == "")
        {
            registerOutputText.text = "Please enter a username";
        }
        else if (_password != _confirmPassword)
        {
            registerOutputText.text = "Passwords do not match";
        }
        else
        {
            var registerTask = auth.CreateUserWithEmailAndPasswordAsync(_email, _password);

//code continues from here

That last line is where the Null Reference error gets thrown whenever I click the "Register" button. (There's a similar line of code in the Login function that throws the same error, so I'm assuming the root is the same.)

What I've done to attempt to fix this:

  • Verified that the json file is in the Assets folder
  • Restarted Unity
  • Deleted and reinstalled the Firebase packages (auth, database, and remote config)

I'm really at a loss here, any help would be greatly appreciated.

2 Answers2

0

looks to me very obviously auth is null and that InitializeFirebase() is not triggered correctly.

0

String.IsNullOrEmpty(String) Method

String.Equals Method

auth is probably null, maybe this helps:

    if (String.IsNullOrEmpty(_username))
    {
      registerOutputText.text = "Please enter a username";
      return;
    }
    
    if (String.IsNullOrEmpty(_password))
    {
      registerOutputText.text = "Please enter a password";
      return;
    }
    
    if (null == auth)
    {
      registerOutputText.text = "auth is null";
      return;
    }        
    
    if (!_password.Equals(_confirmPassword))
    {
      registerOutputText.text = "Passwords do not match";
      return;
    }
    
    var registerTask = auth.CreateUserWithEmailAndPasswordAsync(_email, _password);
  • Based on what you put above, I put this code into the Update: if (null == auth) { Debug.Log("Auth is null"); } The result, unsurprisingly, is that auth is null...and I can't figure out why it won't execute that line of code to initialize Firebase. Any ideas? – Brian Wiggins Nov 04 '21 at 18:16
  • It could be that the method InitializeFirebase wasn't called (in Awake for example) and therefor auth is left as an uninitialized field. Or FirebaseAuth.DefaultInstance returns null, but that would mean auth.StateChanged += AuthStateChanged wouldn't work. – NotDishwasherSafe Nov 05 '21 at 23:28