Search Unity

Modify thing in unity threw Tasks (Firebase authentification)

Discussion in 'External Tools' started by Lockface77, Aug 26, 2019.

  1. Lockface77

    Lockface77

    Joined:
    Jan 31, 2018
    Posts:
    3
    Hello,

    I got an issue with firebase:

    I would like to do a simple register / login with email and password.

    It is working fine for me but when I try to detect when the user typed something wrong or written the a wrong email, it is not working.

    here is my code for the register part:

    Code (CSharp):
    1.     public void RegisterUserEmailAndPassword(string email, string password)
    2.     {
    3.         auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
    4.             if (task.IsCanceled)
    5.             {
    6.                 Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
    7.                 return;
    8.             }
    9.             if (task.IsFaulted)
    10.             {
    11.                 Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
    12.                 Debug.Log("test");
    13.                 ErrorPopUp.PopError("Incorect user or password");
    14.                 Debug.Log("test2");
    15.                 return;
    16.             }
    17.             // Firebase user has been created.
    18.             user = task.Result;
    19.             Debug.LogFormat("Firebase user created successfully: {0} ({1})",
    20.                 user.DisplayName, user.UserId);
    21.         });
    22.     }
    My problem is at the lign "ErrorPopUp.PopError("Incorect user or password");", this normally instantiate an error popup with the text: "Incorect user or password", but it actually does nothing.

    Even more weird here is the console output when I type something wrong:
    upload_2019-8-26_10-27-14.png
    as you can see the "test2" is not printed in the console.

    I hope you could help me please :)

    And before you ask, my ErrorPopUp script works fine everywhere else just not there. Even if I replace it by "Destroy(gameObject)" it does nothing
     
  2. Lockface77

    Lockface77

    Joined:
    Jan 31, 2018
    Posts:
    3
    Hello,

    I Think I know from where my problem come from:

    It is because when I create a user using : "auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith("

    It create a task after, and I am not able to modify thing in unity such as text or instantiate object or destroy when I am inside of a task. Can someone help me with that?

    I've seen that they are some methods which allow wait wait for a task to end, but I don't really understand how to make it work with "auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task => " I mean to get the task outside of the function and wait for it to end.
     
  3. Lockface77

    Lockface77

    Joined:
    Jan 31, 2018
    Posts:
    3
    Well so after a day of research and some prophanation I have found a way to do this:

    But it is very unproper.

    So what I found is that when you create a Task, it creates another thread but this thread is "independ from unity" it mean that some api calls inside of this thread might not work.

    So what I do is I create a public variable called "task finished" I put it to false and when the task is finish I make it true.

    Then I just call my function. My function have this inside :
    Code (CSharp):
    1. yield return new WaitUntil(() => MyVariable == true);
    So I can do some work after it is true.

    Maybe a proper way would be to create an object "ThreadParameter" and just put some options inside it like "finished" or "error" then just take it inside of our fonction and wait the data to get changed inside of the task.

    And if some people want to use my "prophanation" you can but don't forget to call your fonction using:
    Code (CSharp):
    1. StartCoroutine(YourFunctionHere())
    It can save you another 30 min :)

    So maybe I helped some people but this is a very unproper way to deal with this problem I hope Unity can solve the problem of tasks or someone know a better way of doing this.