Search Unity

  1. Click here to receive a gift with your purchase of Unity Pro or Unity Enterprise.
    Dismiss Notice
  2. 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

Changing text on TextMeshPro components changes it on another component instead

Discussion in 'Scripting' started by Tigro, Jul 7, 2020.

  1. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    50
    This is so trivial yet so infurtiating that I have no idea how to progress with it. So I have counters next to my functional buttons in the game, which should go down every time they're used. So here they are in hierarchy of my GUI. I also have some code attached in my "game brain" script which handles the initial values (let's not bother with the changes):

    Code (CSharp):
    1.     void Start() {
    2.         ...
    3.  
    4.         changeCounter(pollNewColours_obj, pollNewColours_counter);
    5.         changeCounter(swapColours_obj, swapColours_counter);
    6.  
    7.         ...
    8.     }
    9.     ...
    10.     private void changeCounter(GameObject obj, int newValue)
    11.     {
    12.         obj.gameObject.GetComponentInChildren<TextMeshProUGUI>().SetText(newValue.ToString());
    13.     }
    And finally, we have the respective objects and counters attached in the brain (don't worry about there being both a reference to the top-level object and the counter itself, it's just because I've tried a lot of combinations to make this work so there are some redundant leftovers).

    Yet when we run it, instead of getting a 1 on the top button and a 3 on the bottom one, we get them reversed. And if we keep playing, they're gonna decrement correctly - but _on the wrong buttons_.

    Why on earth is that happening? I've tried accessing the .text and using SetText(), I've tried going form the top level object and finding the TMP objects through GetComponentInChildren<> and through accessing the component directly, I've even tried putting these into the Update() to be sure nothing overwrites them between the Start() and later methods - all to no avail. No matter what I do, they're always reversed.

    Any help will be greatly appreciated.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,010
    Try putting in these log statements into changeCounter:
    Code (csharp):
    1. Debug.Log($"changeCounter({obj.name}, {newValue})", obj);
    2. Debug.Log("And the text I'm getting is....", obj.gameObject.GetComponentInChildren<TextMeshProUGUI>());
    Now run it. You should see this being called once for each line that calls changeCounter. First, make sure that what you see are the objects you expect. Now, click on the outputs in the console, and make sure the object getting highlighted for each is the object you expect. Also, make sure it's not getting called extra times (it's possible you have an extra copy of the script with wrong references somewhere).

    Also, I guess, double check that the text fields are in the right place? i.e. make sure that the text field that's a child of the bottom bubble is in fact the one that's located in the bottom bubble. I could easily see somebody duplicating it and then dragging the wrong one.
     
    Tigro likes this.
  3. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    50
    Oh my, in the end I found the culprit, it's exactly what you mentioned in your second paragraph! So the problem was that even though everything in the hierarchy was right and the naming was right, somehow the counters themselves were in other positions! So the "* colours" parent objects were right, the "Counter BG" objects were right but the final, deepest objects, despite their correct naming, had their location shifted to the place of their counterparts. Reparented them reversely and it all works now - thank you for the help! :)
     
unityunity