Search Unity

TextMesh Pro From unity UI to TextMeshPro

Discussion in 'UGUI & TextMesh Pro' started by Guich, Mar 30, 2017.

  1. Guich

    Guich

    Joined:
    Jan 16, 2017
    Posts:
    2
    Hi!

    I would like to use TextMeshPro with the space shooter tutorial from unity (
    )
    but I can't seem to make it work.
    I've created a UI Textmesh pro text and change my script:

    using TMPro;

    public class GameController : MonoBehaviour
    {
    public TextMeshPro scoreText;
    void Start ()
    {
    scoreText = GetComponent<TextMeshPro>();
    }
    }

    I can't seem to be able to drag the Text from the hierarchy to the script inside the inspector.
    Any idea why? Am I missing a bigger piece of the puzzle?

    Thank you
     
    quimerico and Stangil1 like this.
  2. TheWanderingBen

    TheWanderingBen

    Joined:
    Nov 3, 2015
    Posts:
    98
    You want to use a TextMeshProUGUI object, like so:
    Code (CSharp):
    1. using TMPro;
    2.  
    3. public class GameController : MonoBehaviour
    4. {
    5.     public TextMeshProUGUI scoreText;
    6.     void Start ()
    7.     {
    8.         scoreText = GetComponent<TextMeshProUGUI>();
    9.     }
    10. }
     
  3. Guich

    Guich

    Joined:
    Jan 16, 2017
    Posts:
    2
    Thank you so much!
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    There are (2) TextMesh Pro components.

    The first is designed to replace the TextMesh component which works with the Mesh Renderer and located in "Create - 3D Object - TextMeshPro - Text". This component is of type <TextMeshPro>. This component is great for world space use.

    Code (csharp):
    1.  
    2. using TMPro;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.      public TextMeshPro TextComponent;
    7.  
    8.      void Start()
    9.      {
    10.           TextComponent = GetComponent<TextMeshPro>();
    11.      }
    12. }

    The second TextMesh Pro component is designed to replace UI.Text which works with the CanvasRenderer and Canvas system and located in "Create - UI - TextMeshPro - Text". This component is of type <TextMeshProUGUI>.

    Code (csharp):
    1.  
    2. using TMPro;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.      public TextMeshProUGUI TextComponent;
    7.  
    8.      void Start()
    9.      {
    10.           TextComponent = GetComponent<TextMeshProUGUI>();
    11.      }
    12. }
    Please note that both TextMesh Pro text components inherit from <TMP_Text> which can conveniently be used when using GetComponent<TMP_Text>();

    Code (csharp):
    1.  
    2. using TMPro;
    3.  
    4. public class Example : MonoBehaviour
    5. {
    6.      public TMP_Text TextComponent;
    7.  
    8.      void Start()
    9.      {
    10.           TextComponent = GetComponent<TMP_Text>();
    11.      }
    12. }
     
  5. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    Any and all of these changes create the console error:

    NullReferenceException: Object reference not set to an instance of an object

    I have been struggling to fix this for a day now, anyone have a solution?
     
    t-heo likes this.
  6. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    FYI:

    It seems you have to have this line in the code in order to avoid the error:

    scoreText = GetComponent<TextMeshProUGUI>() ?? gameObject.AddComponent<TextMeshProUGUI>();
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    If you already have a TextMesh Pro component on the object the contains the script you are using then you only need to get the component.

    Code (csharp):
    1.  
    2. textComponent = GetComponent<TMP_Text>();
    3.  
    Note that both TMP text components (TextMeshPro and TextMeshProUGUI) inherit from TMP_Text so when using GetComponent, you can refer to the base type which is TMP_Text.

    However, when you need to add a component, you need to specify the type which is either the <TextMeshPro> type or the <TextMeshProUGUI> depending on whether you are working with the canvas or mesh renderer.
     
    LexSheyn, Azurne and nobluff67 like this.
  8. nobluff67

    nobluff67

    Joined:
    Nov 3, 2016
    Posts:
    338
    Thanks, you have already answered this for me in another thread.

    I removed the bellow line completely and it worked as expected:

    scoreText = GetComponent<TextMeshProUGUI>() ?? gameObject.AddComponent<TextMeshProUGUI>();
     
    csm03780513 and George-Dolbier like this.