How to call a function inside Firebase email login Task in Unity?

Viewed 237

On My unity app, inside login using email and password, I am using firebase login. My problem is when I try to call a function or execute a group code inside the task nothing happen. Not even an error. How to call a function inside this.

public void signin()
{
    auth.SignInWithEmailAndPasswordAsync(email.text, password.text).ContinueWith(task => {
         if (task.IsCanceled) 
         {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
         if (task.IsFaulted)
         {
        Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
        return;
        }

        Firebase.Auth.FirebaseUser newUser = task.Result;

        //========Checking Email Verified Or Not======

        if(auth.CurrentUser.IsEmailVerified== true)
        {
            Debug.LogFormat("User signed in successfully: {0} ({1})",newUser.DisplayName, newUser.UserId);

         //=================Here is the Problem this is not calling==========
           MyFunction();
         //==============================================
        }
        else
        {
        print("Email Not Verified");
        auth.SignOut(); 
        }
        //===========
       
        });     
}
1 Answers

In general the most common issue with these kind of things is that most of the Unity API can only be used on the main thread.

The Task.ContinueWith is not guaranteed to be executed in the main thread but might happen in any async background thread/task

Therefore Firebase provides an extension method ContinueWithOnMainThread you should use instead, which makes sure the inner codeblock is executed on the Unity main thread.

So simply replace

auth.SignInWithEmailAndPasswordAsync(email.text, password.text).ContinueWith(task => {

with

auth.SignInWithEmailAndPasswordAsync(email.text, password.text).ContinueWithOnMainThread(task => {

make sure you have using Firebase.Extensions at the top of your file

Related