Search Unity

Cannot change text of UI element

Discussion in 'Scripting' started by ldgameworks, Jun 17, 2019.

  1. ldgameworks

    ldgameworks

    Joined:
    May 30, 2019
    Posts:
    5
    Hello everyone,

    I have been absolutely losing my s#!$ over the last couple of days.
    My goal is very simple.

    I have three Text views, and during runtime I use my script to change their text values. Simple as that.

    Yet I can't.


    Code (CSharp):
    1. public class FindGameScene : MonoBehaviour
    2. {
    3.     public Text t1;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         var gameModel =
    9.         new GameItemModel("someGameId", "someGameName", "aGameType");
    10.          
    11.         this.updateView(gameModel,);
    12.      
    13.     }
    14.  
    15.     void updateView(GameItemModel model)
    16.     {
    17.         print("Update started");
    18.      
    19.         print(this.t1.text);
    20. //      this.t1.text = model.gameType;
    21.  
    22.         print("Update finished");
    23.     }
    24.  
    25.     public class GameItemModel
    26.     {
    27.         public string gameId;
    28.         public string gameName;
    29.         public string gameType;
    30.  
    31.         public GameItemModel(string gameId, string gameName, string gameType)
    32.         {
    33.             this.gameId = gameId;
    34.             this.gameName = gameName;
    35.             this.gameType = gameType;
    36.  
    37.         }
    38.     }
    39. }
    This is the code I have.

    When I run it like this, it works fine, meaning the script CAN access the Text object, retrieve its text variable value and print it. Then the 'Update finished' line is printed and I know the method has finished running.

    BUT, once you uncomment the line where I try to set its value, the script just stops running once it gets there. It doesn't terminate, it just seems to hang. The 'Update finished' line does not get printed. Here's that part of the code again, for clarity.

    Code (CSharp):
    1.  
    2. void updateView(GameItemModel model)
    3.     {
    4.         print("Update started");
    5.      
    6.           print(this.t1.text);
    7.           this.t1.text = model.gameType;
    8.  
    9.         print("Update finished");
    10.     }
    I'd appreciate any attempt to help me fix this annoying thing, it has driven me crazy these last couple of days.

    Thanks in advance!

    Update: Initially I used TMP_Text as the type of the elements. I have now changed it to Text, but the problem still persists.
     
    Last edited: Jun 17, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Show the gameType1 field definition. Looks like error is there.
     
  3. ldgameworks

    ldgameworks

    Joined:
    May 30, 2019
    Posts:
    5
    Oh crap, I must have missed that. I updated it now but as a matter of fact, it does not solve the problem.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I'm not sure, but your description sounds like stack overflow or infinite loop. TMP_Text isn't a buildin unity component, so it must be somewhere in int's text setter.
     
  5. ldgameworks

    ldgameworks

    Joined:
    May 30, 2019
    Posts:
    5
    I just changed the elements' types from TMP_Text to Text.
    Nothing changed, problem still persists.
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Obvious question, but still.. are there any errors in console? May be you forgot to set t1 reference in the Inspector?
     
  7. ldgameworks

    ldgameworks

    Joined:
    May 30, 2019
    Posts:
    5
    Nope, no errors whatsoever, it just prints the original contents of the element and then it hangs.
    Reference is set in the inspector; at this point I'm literally re-setting the reference on every script modification.
    It's hella frustrating.
     
  8. ldgameworks

    ldgameworks

    Joined:
    May 30, 2019
    Posts:
    5
    Okay I might have something.

    When I checked the element in the inspector while the game was running, the contents of the text field were changed. But this change is not reflected in the Game panel.

    I have no idea why it could act this way and why it would block the script.