Unity and Firebase, Help with Exception

I have the problem while connecting to db. And I have not see reason why it exception occures:

NullReferenceException: Object reference not set to an instance of an object AuthManager+d__19.MoveNext () (at Assets/Firebase/Scripts/AuthManager.cs:180)

Thats here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Auth;
using Firebase;
using Firebase.Database;
using UnityEngine.UI;

public class AuthManager : MonoBehaviour
{
    public DependencyStatus dependecyStatus;
    public DatabaseReference DBReference;
    [Header("Firebase")]
    public FirebaseAuth auth;
    public static FirebaseUser User;

    [Header("Login")]
    public InputField emailLoginField;
    public InputField passwordLoginField;
    public Text statusLogin;

    [Header("Register")]
    public InputField emailField;
    public InputField passwordFieldRegister;
    public InputField repeatPasswordFieldRegister;
    public Text statusRegister;



    private void Awake()
    {
        InitializeFirebase();
        FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
        {
            dependecyStatus = task.Result;
            if (dependecyStatus == DependencyStatus.Available)
            {
                InitializeFirebase();
            }
            else
            {
                Debug.LogError($"Could not resolve all Firebase dependencies:{dependecyStatus}");

            }
        });
    }
    private void InitializeFirebase()
    {
        Debug.Log("Setting up Firebase Auth");

        auth = FirebaseAuth.DefaultInstance;
        DBReference = FirebaseDatabase.DefaultInstance.RootReference;
        if(DBReference== null) {
            Debug.Log("Empty1");
        }
    }

    public void LoginButton()
    {
        StartCoroutine(Login(emailLoginField.text.ToString(), passwordLoginField.text.ToString()));
    }

    public void RegisterButton()
    {
        StartCoroutine(Register(emailField.text, passwordFieldRegister.text));
    }

    public IEnumerator Login(string email, string password)
    {
        var LoginTask = auth.SignInWithEmailAndPasswordAsync(email, password);
        yield return new WaitUntil(predicate: () => LoginTask.IsCompleted);
        if (LoginTask.Exception != null)
        {
            FirebaseException firebaseException = LoginTask.Exception.GetBaseException() as FirebaseException;
            AuthError errorCode = (AuthError)firebaseException.ErrorCode;
            string message = "Login Failed:";
            switch (errorCode)
            {
                case AuthError.MissingEmail:
                    message += "Missing Email";
                    break;
                case AuthError.MissingPassword:
                    message += "Missing Password";
                    break;
                case AuthError.WrongPassword:
                    message += "Wrong Password";
                    break;
                case AuthError.InvalidEmail:
                    message += "Invalid Email";
                    break;
                case AuthError.UserNotFound:
                    message += "Account does not exist";
                    break;
            }
            statusLogin.text = message;
        }
        else
        {
            User = LoginTask.Result;
            Debug.LogFormat($"User signed Successfully {User.DisplayName}, {User.Email}");
            PlayerPrefs.SetString("Email", email);
            PlayerPrefs.SetString("Password", password);
            statusLogin.text = "Logged in";
        }
    }
    public IEnumerator Register(string email, string password)
    {
        if (email == "")
        {
            statusRegister.text = "Missing e-mail";
        }
        else if (repeatPasswordFieldRegister.text != passwordFieldRegister.text)
        {
            statusRegister.text = "Password does not match";
        }
        else
        {
            var RegisterTask = auth.CreateUserWithEmailAndPasswordAsync(email, password);
            yield return new WaitUntil(predicate: () => RegisterTask.IsCompleted);

            if (RegisterTask.Exception != null)
            {
                Debug.LogWarning(message: $"Failed to register task with {RegisterTask.Exception}");
                FirebaseException firebaseException = RegisterTask.Exception.GetBaseException() as FirebaseException;
                AuthError errorCode = (AuthError)firebaseException.ErrorCode;

                string message = "Register Failed:";
                switch (errorCode)
                {
                    case AuthError.MissingEmail:
                        message += "Missing Email";
                        break;
                    case AuthError.MissingPassword:
                        message += "Missing Password";
                        break;
                    case AuthError.WeakPassword:
                        message += "Weak Password";
                        break;
                    case AuthError.EmailAlreadyInUse:
                        message += "Email already in use";
                        break;
                }
                statusRegister.text = message;
            }
            else
            {
                User = RegisterTask.Result;
                if (User != null)
                {
                    UserProfile profile = new UserProfile { DisplayName = email };

                    var ProfileTask = User.UpdateUserProfileAsync(profile);
                    yield return new WaitUntil(predicate: () => ProfileTask.IsCompleted);
                    if (ProfileTask.Exception != null)
                    {
                        Debug.LogWarning(message: $"Failed to register task with {ProfileTask.Exception}");
                        FirebaseException firebaseException = ProfileTask.Exception.GetBaseException() as FirebaseException;
                        AuthError errorCode = (AuthError)firebaseException.ErrorCode;
                        statusRegister.text = "Username set failed";
                    }
                    else
                    {
                        FindObjectOfType<UIManager>().SwitchToLogin();
                        statusRegister.text = "";
                    }
                }
            }
        }
    }
    IEnumerator Setter(object data)
    {
        var DBTask = DBReference.Child("users").Child(AuthManager.User.UserId).Child("saves").SetValueAsync(data);
        yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
    }
    void OnApplicationQuit()
    {
        SaveSystem.obj.saveItem();
        StartCoroutine(Setter(SaveSystem.obj));
    }

    public IEnumerator GetTenLessDeathTaker()
    {

        var DBTask = DBReference.Child("users").OrderByChild("deathCount").GetValueAsync();
        yield return new WaitUntil(predicate: () => DBTask.IsCompleted);
        //new WaitUntil(predicate: () => reference.IsCompleted);
        //if (DBTask.Result.Value != null)
        //{
        //    List<DataSnapshot> snapshots = new List<DataSnapshot>(DBTask.Result.Children);
        //    //snapshots = snapshots.Take(10).ToList();
        //    for (int i = 0; i < snapshots.Count; i++)
        //    {
        //        var top10 = FindObjectOfType<UIManager>().GetComponent<RectTransform>().GetChild(1);
        //        var span = Resources.Load("Launcher/Leader") as GameObject;
        //        span.GetComponent<Text>().text = snapshots[i].Child("saves").Child("deathCount").Value.ToString();
        //        Instantiate(span, Vector3.zero, Quaternion.identity, top10.GetComponent<RectTransform>());
        //    }
        //}
    }
    private void UIManager_ValueChanged(object sender, ValueChangedEventArgs e)
    {
        Debug.Log(e.Snapshot.Child("users").GetValue(true));
    }
}

Your post has nothing to do with 2D so this is obviously the wrong forum.

I’ll move your post for you.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

It’s no different if it isn’t your code. Use the details you find by Step #1 above to reason about how you might be misusing the other code.

Steps to solving a null reference error are always the same.

  1. Find out what is null
  2. Find out why it’s null
  3. Fix it.

Your error points to line 180, but your pasted code can’t have 180 be null, since it’s a blank line. So, we need to guess that since it mentions MoveNext, that it’s a line in your coroutine. So either 184 or 185.

https://firebase.google.com/docs/database/unity/retrieve-data
Look over how to retrieve data from Firebase’s database.

I have a look that DBReference is null, how it possible could be? FirebaseAuth works well, but realtime db isn’t work…

Time to start debugging! Guess you need to make sure the code is getting hit as you expect and your database is setup correctly!