# Access TextMeshPro text through script?

**URL:** <https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157>\
**Category:** UGUI\
**Tags:** UGUI\
**Created:** [April 22, 2018, 4:10pm UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157 "2018-04-22T16:10:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ch33ri0s](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ch33ri0s/32/195423_2.png) [@Ch33ri0s](https://discussions.unity.com/u/Ch33ri0s)\
**Post date:** [April 22, 2018, 4:10pm UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/1 "2018-04-22T16:10:24Z")

</div>

On a typical text UI component I would just use “Text” and make it a public variable. I just need to know how to change the text using textmesh pro? Or am I better off just using a text UI component for this?

Thanks.

---

<div class="post-metadata">

**Author:** ![Stephan\_B](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@Stephan\_B](https://discussions.unity.com/u/Stephan_B)\
**Post date:** [April 22, 2018, 6:47pm UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/2 "2018-04-22T18:47:05Z")

</div>

There are two TMP components who both derive from TMP\_Text. They are both in the TMPro namespace.

The first component of type is designed to replace the old TextMesh which uses the MeshRenderer.

The 2nd of type is designed to replace UI.Text and designed to work with the CanvasRenderer and Canvas system.

This information is also available in the [FAQ - Question 9](http://digitalnativestudios.com/forum/index.php?topic=890.0) on the TextMesh Pro user forum.

```csharp
using UnityEngine;
/// Include the name space for TextMesh Pro
using TMPro;

public class Example : MonoBehaviour
{
    private TMP_Text m_TextComponent;

    private void Awake()
    {
        // Get a reference to the text component.
        // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
        m_TextComponent = GetComponent<TMP_Text>();

        // Change the text on the text component.
        m_TextComponent.text = "Some new line of text.";
    }
}

```

---

<div class="post-metadata">

**Author:** ![Ch33ri0s](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/ch33ri0s/32/195423_2.png) [@Ch33ri0s](https://discussions.unity.com/u/Ch33ri0s)\
**Post date:** [April 22, 2018, 7:36pm UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/3 "2018-04-22T19:36:20Z")

</div>

> [@Stephan\_B](#):
>
> There are two TMP components who both derive from TMP\_Text. They are both in the TMPro namespace.
> 
> The first component of type is designed to replace the old TextMesh which uses the MeshRenderer.
> 
> The 2nd of type is designed to replace UI.Text and designed to work with the CanvasRenderer and Canvas system.
> 
> This information is also available in the [FAQ - Question 9](http://digitalnativestudios.com/forum/index.php?topic=890.0) on the TextMesh Pro user forum.
> 
> ```csharp
> using UnityEngine;
> /// Include the name space for TextMesh Pro
> using TMPro;
> 
> public class Example : MonoBehaviour
> {
> private TMP_Text m_TextComponent;
> 
> private void Awake()
> {
> // Get a reference to the text component.
> // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
> m_TextComponent = GetComponent<TMP_Text>();
> 
> // Change the text on the text component.
> m_TextComponent.text = "Some new line of text.";
> }
> }
> 
> ```

Exactly the information I needed. Thanks!

---

<div class="post-metadata">

**Author:** ![nadine505](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@nadine505](https://discussions.unity.com/u/nadine505)\
**Post date:** [September 7, 2019, 6:41am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/4 "2019-09-07T06:41:29Z")

</div>

The VS editor says using the TMPro directive is unnecessary, yet the TMP\_Text type could not be found. I am using Unity 2018.4.3 so maybe this is done differently in this version of Unity?

---

<div class="post-metadata">

**Author:** ![Stephan\_B](https://avatars.discourse-cdn.com/v4/letter/s/85e7bf/32.png) [@Stephan\_B](https://discussions.unity.com/u/Stephan_B)\
**Post date:** [September 7, 2019, 7:49am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/5 "2019-09-07T07:49:25Z")

</div>

> [@nadine505](#):
>
> The VS editor says using the TMPro directive is unnecessary, yet the TMP\_Text type could not be found. I am using Unity 2018.4.3 so maybe this is done differently in this version of Unity?

Nothing has changed in terms of the requirement to use “using TMPro;”

---

<div class="post-metadata">

**Author:** ![garethdenyer](https://avatars.discourse-cdn.com/v4/letter/g/e56c9b/32.png) [@garethdenyer](https://discussions.unity.com/u/garethdenyer)\
**Post date:** [September 1, 2020, 8:38am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/6 "2020-09-01T08:38:42Z")

</div>

This was very helpful and has allowed me to attached live text to all sorts of objects, updating during the game.

I did need the namespace “using TMPro” but I didn’t seem to need the step “private TMP\_Text m\_TextComponent;”

So having declared the TMP\_Text variable with the line “public TMP\_Text theTMPgameobject;” I was then able to populate the text with "theTMPgameobject.text = “the text I wanted here”;

Fantastic stuff… many thanks!!

---

<div class="post-metadata">

**Author:** ![naxs](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@naxs](https://discussions.unity.com/u/naxs)\
**Post date:** [June 24, 2021, 3:29am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/7 "2021-06-24T03:29:06Z")

</div>

> [@Stephan\_B](#):
>
> There are two TMP components who both derive from TMP\_Text. They are both in the TMPro namespace.
> 
> The first component of type is designed to replace the old TextMesh which uses the MeshRenderer.
> 
> The 2nd of type is designed to replace UI.Text and designed to work with the CanvasRenderer and Canvas system.
> 
> This information is also available in the [FAQ - Question 9](http://digitalnativestudios.com/forum/index.php?topic=890.0) on the TextMesh Pro user forum.
> 
> ```csharp
> using UnityEngine;
> /// Include the name space for TextMesh Pro
> using TMPro;
> 
> public class Example : MonoBehaviour
> {
> private TMP_Text m_TextComponent;
> 
> private void Awake()
> {
> // Get a reference to the text component.
> // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
> m_TextComponent = GetComponent<TMP_Text>();
> 
> // Change the text on the text component.
> m_TextComponent.text = "Some new line of text.";
> }
> }
> 
> ```

Thank you very much.

---

<div class="post-metadata">

**Author:** ![sm0k351](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@sm0k351](https://discussions.unity.com/u/sm0k351)\
**Post date:** [September 4, 2021, 6:46pm UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/8 "2021-09-04T18:46:29Z")

</div>

Thanks!!!

---

<div class="post-metadata">

**Author:** ![fuzzy3d](https://avatars.discourse-cdn.com/v4/letter/f/a9a28c/32.png) [@fuzzy3d](https://discussions.unity.com/u/fuzzy3d)\
**Post date:** [May 11, 2022, 8:19am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/9 "2022-05-11T08:19:23Z")

</div>

> [@Stephan\_B](#):
>
> This information is also available in the [FAQ - Question 9](http://digitalnativestudios.com/forum/index.php?topic=890.0) on the TextMesh Pro user forum.

link is broken

---

<div class="post-metadata">

**Author:** ![Rachan](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/rachan/32/83838_2.png) [@Rachan](https://discussions.unity.com/u/Rachan)\
**Post date:** [September 11, 2022, 3:53am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/10 "2022-09-11T03:53:39Z")

</div>

> [@Stephan\_B](#):
>
> There are two TMP components who both derive from TMP\_Text. They are both in the TMPro namespace.
> 
> The first component of type is designed to replace the old TextMesh which uses the MeshRenderer.
> 
> The 2nd of type is designed to replace UI.Text and designed to work with the CanvasRenderer and Canvas system.
> 
> This information is also available in the [FAQ - Question 9](http://digitalnativestudios.com/forum/index.php?topic=890.0) on the TextMesh Pro user forum.
> 
> ```csharp
> using UnityEngine;
> /// Include the name space for TextMesh Pro
> using TMPro;
> 
> public class Example : MonoBehaviour
> {
> private TMP_Text m_TextComponent;
> 
> private void Awake()
> {
> // Get a reference to the text component.
> // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
> m_TextComponent = GetComponent<TMP_Text>();
> 
> // Change the text on the text component.
> m_TextComponent.text = "Some new line of text.";
> }
> }
> 
> ```

Thanks!!!

---

<div class="post-metadata">

**Author:** ![WillJohnson27534](https://avatars.discourse-cdn.com/v4/letter/w/bbce88/32.png) [@WillJohnson27534](https://discussions.unity.com/u/WillJohnson27534)\
**Post date:** [December 19, 2022, 7:33am UTC](https://discussions.unity.com/t/access-textmeshpro-text-through-script/699157/11 "2022-12-19T07:33:02Z")

</div>

> [@Stephan\_B](#):
>
> There are two TMP components who both derive from TMP\_Text. They are both in the TMPro namespace.
> 
> The first component of type is designed to replace the old TextMesh which uses the MeshRenderer.
> 
> The 2nd of type is designed to replace UI.Text and designed to work with the CanvasRenderer and Canvas system.
> 
> This information is also available in the [FAQ - Question 9](http://digitalnativestudios.com/forum/index.php?topic=890.0) on the TextMesh Pro user forum.
> 
> ```csharp
> using UnityEngine;
> /// Include the name space for TextMesh Pro
> using TMPro;
> 
> public class Example : MonoBehaviour
> {
> private TMP_Text m_TextComponent;
> 
> private void Awake()
> {
> // Get a reference to the text component.
> // Since we are using the base class type <TMP_Text> this component could be either a <TextMeshPro> or <TextMeshProUGUI> component.
> m_TextComponent = GetComponent<TMP_Text>();
> 
> // Change the text on the text component.
> m_TextComponent.text = "Some new line of text.";
> }
> }
> 
> ```

you’re a lifesaver my guy
