Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted Label text does not change when text parameter is modified by a script

Discussion in 'UI Toolkit' started by sevaliaroshan, Oct 9, 2020.

  1. sevaliaroshan

    sevaliaroshan

    Joined:
    Feb 2, 2019
    Posts:
    11
    I have a pause menu with a title label that displays who paused the game. There is some placeholder text that my script replaces like so where "playerNameText" is the title label and the "canvas" is the UIDocument. component.

    playerNameText.text = playerName + " Paused";
    canvas.enabled = true;


    I used a debugger to step through and what I found is that after the text is changed, the text property of the label remains on what I set it to, but the actual visible label does not update in game. I have other UI that uses labels that have their text changed this way and it works fine. None of my buttons in this UI trigger any code either despite having clicked actions bound in the start function (lines are executed as confirmed by the debugger).
     
  2. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    43
    Hi,

    If you use the UIToolkit debugger (window-> UiToolkit -> debugger) and that you select the element (You cannont "pick" runtime element for now, you will need to select the PanelSetting name in the top left dropdown and explore your hierarchy manually), do you see the updated text or the placeholder? Normally both should be in sync and if they don't, please let me know because it would be a bug.

    I think it may be a question of timing and you may explore the following to get a workaround:
    • Can you change "playerNameText.text" at the same time you change the other labels that are working?
    • Can you change the other labels text at the place where you change the playerNameText?
    • Can you enable the canvas before you change the text and see if there is a difference?

    Let me know once you have any results.
     
  3. sevaliaroshan

    sevaliaroshan

    Joined:
    Feb 2, 2019
    Posts:
    11
    I checked the debugger and the text was not being changed. I have tried before and after enabling the canvas and the result is the same. I will continue to investigate the timing thing, but this seems to happen specifically when enabling the ui document at the same time as changing the text. If I enable/disable the whole gameobject and leave the UI document component enabled it seems to work fine, as that is how my main menu works. The same thing seems to be true with the buttons where disabling the UI document component seems to mess with the button actions, they work fine when the whole gameobject starts disabled and is enabled though.
     
  4. sevaliaroshan

    sevaliaroshan

    Joined:
    Feb 2, 2019
    Posts:
    11
    I did a bit more investigation with other UI I have, and I found that if the UI Document component itself gets disabled at any point during runtime, even if its through the gameobject (gameObject.SetActive), it malfunctions and stops registering any changes I make in code when reenabled. I have Mirror for networking and when an object I had went through a network switch (disables and reenables the gameobject in the same or next frame), the UI on that gameobject stopped registering changes (not shown in the UI toolkit debugger either), though print statements & vscode debugger show the statements are going through in code.
     
  5. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    43
    Thanks for the investigation.

    It really looks like there is a problem if disabling-enabling the game object changes break the internal cache.

    Can you file a bug for this? Having clear reproduction step or an example project will help the team fix it quickly.
     
  6. sevaliaroshan

    sevaliaroshan

    Joined:
    Feb 2, 2019
    Posts:
    11
    Am I supposed to submit it through Help -> report a bug in editor?
     
  7. broots

    broots

    Joined:
    Dec 20, 2019
    Posts:
    6
    I encountered the same issue (for renaming an object field) and found a cheaty workaround that might work in your case as well where you set the object as null and then reassign it:

    Code (CSharp):
    1.            
    2. charSelector.value = null;
    3. charSelector.value = currentCharacter;
    4.  
     
  8. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    43
    Yes :) Let me know if you need help with this.
     
  9. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    111
    Hello! The fact that the whole UIDocument gets recreated on disable/re-enable is as designed because of Live Reload (more info here: https://forum.unity.com/threads/feeback-wanted-ui-assets-live-reload.963129/)
    Doesn't mean it has to stay that way, but it's not exactly a bug as it is right now.
    The recommended flow is that you set your values with a companion MonoBehaviour to your UIDocument, and use its OnEnable method to do so. You can access the root element from the UIDocument there.
    Can you try this and let us know if this works for you? Or what is the alternative flow you wish to use?
    Thanks!
     
  10. sevaliaroshan

    sevaliaroshan

    Joined:
    Feb 2, 2019
    Posts:
    11
    I have a companion script on the same gameobject as a ui document which serves as a sort of UI controller. I want to be able to turn the UI on and off without disabling the gameobject because I need the script to remain active. If this is not a bug, then recreating the UI document should at least delete the old objects and throw a nullreference when I try to operate on them, because right now no errors are thrown. As for workflow, I think it should be made more clear that you need to rebuild your c# references every time you reenable the UI Document. Ideally we should be able to turn the UIDocument on and off without having problems like UGUI and the canvas component. I have found a workaround for now which is setting the root visual element's visibility to false to give the same effect.
     
unityunity