Firebase Authentication With Unity-对象引用未设置为对象实例



我的Unity手机游戏使用Firebase身份验证。我的代码完全是从文档中复制的,但在尝试运行游戏时出现了错误。

private void Login(string _email, string _password)
{
Debug.Log(_email);
Debug.Log(_password);
//Call the Firebase auth signin function passing the email and password
auth.SignInWithEmailAndPasswordAsync(_email, _password).ContinueWith(task =>    //error line
{
if (task.IsCanceled)
{
Debug.Log("Task Canceled");
return;
}
if (task.IsFaulted)
{
Debug.Log(task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("Sign in done",
newUser.DisplayName, newUser.UserId);
});
//Wait until the task completes

}

错误似乎是在初始化此函数中的auth期间:

void InitializeFirebase()
{
auth = FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);
}

但我找不到解决问题的方法。

您应该在调用方法之前初始化firebase。

FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
if (task.Result == DependencyStatus.Available)
//your code here
else
Debug.LogError($"Could not resolve all Firebase dependencies: {task.Result}");      
});

您还应该确保本地auth变量已正确初始化。

最新更新