Search Unity

TMPro text changing script not working as expected

Discussion in 'UGUI & TextMesh Pro' started by EOMedvis, May 31, 2019.

  1. EOMedvis

    EOMedvis

    Joined:
    Feb 19, 2019
    Posts:
    94
    I'm new the C# Coding in Unity. Could someone point me to the reason why version 1 of the follow code works, but version 2 doesn't, when they should be the same?

    I have a script that changes the text of a textmeshpro text. qBox is a gameobject I put into the editor, this is not changed between the scripts.

    V1:
    Code (CSharp):
    1. void ChangeText() //Changes text.
    2.     {
    3.         TextMeshProUGUI mText = qBox.GetComponent<TextMeshProUGUI>();    
    4.         mText.text = Scenarios.Scenarios[Scenario1Tracker.sNum,Scenario1Tracker.qNum].qText;    
    5.     }
    V2:
    Code (CSharp):
    1. void ChangeText() //Changes text.
    2.     {
    3.         string mText = qBox.GetComponent<TextMeshProUGUI>().text;    
    4.         mText = Scenarios.Scenarios[Scenario1Tracker.sNum,Scenario1Tracker.qNum].qText;    
    5.     }
    The two should be the same, but the first works and if I change it to the second, it stops working. If I undo the changes, it works again. I don't get any errors in the Unity Editor or in Visual Studios.

    Thanks in advance. :)