Search Unity

TextMeshPro weird animation

Discussion in 'Animation' started by SMT5015, May 30, 2021.

  1. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    I tried to animate a message screen with TMP text so it would not pop up too suddenly, but it behaves weirdly.

    The code for message is this:
    Code (CSharp):
    1. public IEnumerator ShowMessage(float duration, VerticalAlignmentOptions vertical, HorizontalAlignmentOptions horizontal, string text)
    2.     {
    3.         //The object starts inactive
    4.         messageBackground.gameObject.SetActive(true);
    5.         //this animation changes alpha for text and bacground from zero to desired values
    6.         messageBackground.gameObject.GetComponent<Animator>().Play("Show");
    7.  
    8.         //set the text, obviously
    9.         message.verticalAlignment = vertical;
    10.         message.horizontalAlignment = horizontal;
    11.         message.text = text;
    12.  
    13.         yield return new WaitForSeconds(duration);
    14.         //Set the colors' alpha to zero, has a behaviour script with disabling the object OnStateExit()
    15.         messageBackground.gameObject.GetComponent<Animator>().Play("Disappear");
    16.     }
    It works normally when used for the first time, but if called more than once per run, the text's alpha switches to zero after the starting animation had been finished. Telling directly to hold the alpha through transition from "show" to another animation which does nothing but this didn't help. It even seemed to work stable for some time and broke out of nowhere.

    How can I fix this or is there other solutions?
     
  2. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Upd.

    This behaviour is triggered by the "Disappear" animation, since removing it from the script in favor of deactivating the object removed also the problem. Also, when I created the animation, text also did not want to follow commands and become transparent, and I still don't know what exactly made it to do so.
     
  3. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Upd 2.

    Guess the entire approach with hiding an object for various messages instead of instantiating and destroying every one anew is flawed. If so, the thread is not needed anymore.