change text colour after animating

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 OnChangeColour 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??

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

public class ChangeColour : MonoBehaviour {

    Color originalColor;
    Text t;
    void Start()
    {
        t = GetComponent<Text>();
        originalColor = t.color;
    }
    public void OnChangeColour(string c)
    {
        if (c == "white")
        {
            t.color = Color.white;
        }
        if (c == "black")
        {
            t.color = Color.black;
        }
    }
    void OnDisable()
    {
        t.color = originalColor;
    }
}

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 :confused: In general disabling Animators is a good idea to avoid needless UI layouting calls.

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.