[2019.2.6f1] UI Text.text not updating properly and halting the code execution

Hello.
I’m developing an Android application that is supposed to interact with an external server through websockets (implemented here using websocketsharp).
Currently, I’m implementing a simple autentication screen that also serves as the place to initialize the client’s websocket:


The idea is:
The user fills in with the login credentials and presses login which in turn initializes the websocket connection with the remote server. The communication establishment between client and server works as intended (so far), triggering the following handler method for it:

private void _OnSocketOpen(object sender, EventArgs e)
    {
        Debug.Log("Connection Confirmed!");
        AutenticationScreen.instance.loginStatusText.text = "Evaluating Credentials...";
        CredentialsMessage message = new CredentialsMessage { ID = "Credentials", Credentials = AutenticationScreen.instance.credentials };
        socket.Send(JsonUtility.ToJson(message));
    }

As you may deduce, this accesses the singleton for the Autentication Screen’s controller script to fetch the credentials. This also works properly.

The problem I’m currently having is with Line 4. I’m trying to modify the text content of the login status UI Text through the script and while it updates the value on the Inspector, it simply does not visually update while also halting the function with no errors thrown at that same line. The code works fine if I remove it (only for it to halt once again when the login evaluation result is returned from the server and I want to set that text to tell whether or not it was successfull). Updating any value on the Text component’s inspector makes it appear which leads me to believe that this is a similar error to this although the issue still seems unresolved and then code does not seem to be halting there

Am I missing something in the way the UI functions here? Is this a bug?

After A LOT of trial and error, I ended up figuring it out: it is a threading issue between the Websocketsharp library and Unity’s UI thread.
I’m trying to find a better alternative than having to use Update() but right now I solved having the text update happen entirely on the Autenticatiion Screen’s script (which in turn removes it from Websocket’s thread) via a string flag. Whenever the string flag is anything but empty, it will update the status text with itself:

    public string loginStatusUpdate;
    public Text loginStatusText;
    private void Update()
    {
        if (loginStatusUpdate!="")
        {
            loginStatusText.text = loginStatusUpdate;
            loginStatusUpdate = "";
        }
    }
1 Like

Hey Joaomlo,

I am working on a similar application and am also having trouble with incoming WS messages not updating the Unity UI or game Scene. Did you find any other solution except for doing everything in the update function?
Any help would be greatly appreciated!

Thanks in advance (: