Using TMPro in script

So i’m trying out this typewriter effect For my splash screen and you know how ugly and crappy Unity’s default ext is so i tried using textmeshpro to counter that but uh…
It doesn’t work…
This is my original script…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TypewriterEffectIntro : MonoBehaviour
{
        Text txt;
    string story;

    void Awake ()
    {
        txt = GetComponent<Text> ();
        story = txt.text;
        txt.text = "";

        // TODO: add optional delay when to start
        StartCoroutine ("PlayText");
    }

    IEnumerator PlayText()
    {
        foreach (char c in story)
        {
            txt.text += c;
            yield return new WaitForSeconds (0.125f);
        }
    }

And this what i tried to make it compatible with TMPro

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.TMPro;

public class TypewriterEffectIntro : MonoBehaviour
{
        TextMeshProText txt;
    string story;

    void Awake ()
    {
        txt = GetComponent<Text> ();
        story = txt.text;
        txt.text = "";

        // TODO: add optional delay when to start
        StartCoroutine ("PlayText");
    }

    IEnumerator PlayText()
    {
        foreach (char c in story)
        {
            txt.text += c;
            yield return new WaitForSeconds (0.125f);
        }
    }

I created the TMPro one by searching on google
But the typewriter effect didn’t work when i did that…
:frowning:

I’m curious where you found your search result at.

TextMeshPro gui objects are of type
TextMeshProUGUI, so you need to use that variable type and also get that component if you aren’t going to drag and drop.

TextMeshProUGUI txt;

void Awake()
{
   txt = GetComponent<TextMeshProUGUI>();
}

Also it is a VERY bad idea to concatenate strings. You’re just feeding the garbage collector with a ton of abandoned string objects. Learn how to use StringBuilder instead.

BTW: I like this more personally:

1 Like

@ Cool link.

For OP, TextMeshPro isn’t like a normal Unity Text object and has a lot more info. Here is a typewriter effect example using TextMeshPro

which simply defines how many characters should be visible.

And there are also ways to add a mask that can simulate a fade effect over the text if you want a fade, but generally if you are going for a typewriter, it wouldn’t fade.

you sound familiar…
have you helped me out before?

TMP_Text is the way to go. TextMeshProUGUI is the whole component with the ui fields and accesses etc.
TMP_Text is only the text part of it.

well that worked for me so i ain’t complaining