Search Unity

change text colour after animating

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by hawken, Apr 3, 2018.

  1. hawken


    Aug 22, 2013
    I'm animating a button to fade in, it animates the alpha value of the button image and the child text colour alpha.

    After the button has faded in, the animation stops playing (it is not set to loop)

    Then when the user clicks on the button, I am using event trigger pointerdown to run a script on the text, a public function called OnChangeColour.

    A string of the colour I want is passed to this.

    With no animator on the button this works fine, but if it uses an animator, the colour can't change.

    Things I have tried:
    • If I target the animator and disable it on
      the colour changes in the inspector but not on the text.
    • Made a new empty state in animator and switch to that after the fader has played, doesn't let text change colour still.

    Fading in ui elements and doing mouse over buttons is 101 basic UI, how can we change colours after animating UI??

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    6. public class ChangeColour : MonoBehaviour {
    8.     Color originalColor;
    9.     Text t;
    10.     void Start()
    11.     {
    12.         t = GetComponent<Text>();
    13.         originalColor = t.color;
    14.     }
    15.     public void OnChangeColour(string c)
    16.     {
    17.         if (c == "white")
    18.         {
    19.             t.color = Color.white;
    20.         }
    21.         if (c == "black")
    22.         {
    23.             t.color =;
    24.         }
    25.     }
    26.     void OnDisable()
    27.     {
    28.         t.color = originalColor;
    29.     }
    30. }
    Last edited: Apr 3, 2018
  2. attilam


    Jun 2, 2007
    I think if a state is set to animate a property it will keep doing so while it is active. Transition into a different state that doesn't animate that property. Also, iirc the state of the 'Write Defaults' switch makes a difference. If it's on the Animator will force the default value on everything on every frame.

    Worst case scenario disable the animator :/ In general disabling Animators is a good idea to avoid needless UI layouting calls.
  3. del_unity


    Jan 23, 2018
    Write Defaults doesn't seem to make any difference, on or off it still writes all property values even when the animation you are playing doesn't use that property... Switching animators on and off is the only solution that I managed to get to work but it is a huge pain to have to do this for buttons all over the UI.

    Please fix this.