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));
}
}