Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

TextMesh Pro TextMesh Pro: Text shall disappear after x seconds it has appeared

Discussion in 'Unity UI & TextMesh Pro' started by florianfeiler2, May 16, 2018.

  1. florianfeiler2

    florianfeiler2

    Joined:
    May 7, 2018
    Posts:
    7
    Hey folks, I'm using the TextMesh Pro Plugin and I want a that the text disappears after x seconds it completly appeared. I tried the following code, but it immediatley delets the letters.
    Code (CSharp):
    1. public class Dialog : MonoBehaviour
    2. {
    3.     public TextMeshProUGUI textDisplay;
    4.     public string[] sentences;
    5.     private int index;
    6.     public float typingSpeed;
    7.     public GameObject continueButton;
    8.     public Animator textDisplayAnim;
    9.  
    10.     void Start()
    11.     {
    12.         StartCoroutine(Type());
    13.     }
    14.     void Update()
    15.     {
    16.        
    17.         if(textDisplay.text == sentences[index])
    18.         {
    19.             continueButton.SetActive(true);
    20.         }
    21. //Here I tried to let the text go away, but it immediatley deletes the first letter which was written
    22.         if(sentences.Length - 1 == index)
    23.         {
    24.  
    25.             textDisplay.text = "";
    26.        StartCoroutine(Type());
    27. }
    28.  
    29.     }
    30.     IEnumerator Type()
    31.     {
    32.         foreach(char letter in sentences[index].ToCharArray())
    33.         {
    34.             textDisplay.text += letter;
    35.             yield return new WaitForSeconds(typingSpeed);
    36.         }
    37.     }
    38.    
    39.     public void NextSentence()
    40.     {
    41.         textDisplayAnim.SetTrigger("change");
    42.         continueButton.SetActive(false);
    43.         if(index < sentences.Length - 1)
    44.         {
    45.             index++;
    46.             textDisplay.text = "";
    47.             StartCoroutine(Type());
    48.         }
    49.         else
    50.         {
    51.             textDisplay.text = "";
    52.             continueButton.SetActive(false);
    53.         }
    54.     }
    55. }
    56.  
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,995
    You want to avoid doing any string concatenation as this generates Garbage Collection. See example 17 Old Computer Terminal included with TextMesh Pro. This uses the maxVisibleCharacter property.

    Also take a look at this thread which also includes a link to a post that contains a script to control fading of characters over time.