Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'UGUI & TextMesh Pro' started by joaomlo, Nov 14, 2019.

  1. joaomlo

    joaomlo

    Joined:
    Sep 27, 2019
    Posts:
    2
    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:​
    upload_2019-11-14_14-33-24.png
    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:

    Code (CSharp):
    1. private void _OnSocketOpen(object sender, EventArgs e)
    2.     {
    3.         Debug.Log("Connection Confirmed!");
    4.         AutenticationScreen.instance.loginStatusText.text = "Evaluating Credentials...";
    5.         CredentialsMessage message = new CredentialsMessage { ID = "Credentials", Credentials = AutenticationScreen.instance.credentials };
    6.         socket.Send(JsonUtility.ToJson(message));
    7.     }
    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?
     
  2. joaomlo

    joaomlo

    Joined:
    Sep 27, 2019
    Posts:
    2
    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:

    Code (CSharp):
    1.     public string loginStatusUpdate;
    2.     public Text loginStatusText;
    3.     private void Update()
    4.     {
    5.         if (loginStatusUpdate!="")
    6.         {
    7.             loginStatusText.text = loginStatusUpdate;
    8.             loginStatusUpdate = "";
    9.         }
    10.     }
     
    fhecorrea likes this.
  3. PeachesWine

    PeachesWine

    Joined:
    Sep 23, 2019
    Posts:
    1
    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 :)