I'm working in a school project using Unity and Firebase where I need to display the username of the current user in a Text box.

I´m already get the data from Firebase but when I try to set the userNameText's text to it, nothing happens.

This is my code:

Variables:

private DatabaseReference reference;
private string userName;
public Text userNameText; 

Initialization:

void Start()
    {
        //database reference (I'm hiding the project id)
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://projectID.firebaseio.com/");

        // Get the root reference location of the database.
        reference = FirebaseDatabase.DefaultInstance.RootReference;

        GetUserData();
    }

GetUserData():

public void GetUserData()
    {
        reference.Child("users").Child(PlayerPrefs.GetString("userUID")).GetValueAsync().ContinueWith(task =>
        {
            if (task.IsFaulted)
            {
                Debug.Log("something was wrong");
            }
            else if (task.IsCompleted)
            {
                Dictionary<string, object> results = (Dictionary<string, object>)task.Result.Value;
                userName= (string)results["userName"];
                Debug.Log(userName);
                userNameText.text = userName;
            }
        });
    }

When I run the project, it just display the userName in the console but the text box has not yet been updated.

0 Answers