Insert script into objects without OnClick

Hello,

I’ve created a set of buttons and want to change the color of text when button is highlighted only.

To avoid single PointerEnter / PointerExit Handler scripting I’ve prepared one “global” script to change highlight color of button included both handlers inside:

public class ColourChanger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{ 
    public void OnPointerEnter(PointerEventData eventData)
    {
        GetComponent<Text>().color = new Color(255, 100, 20); // highlighted color
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        GetComponent<Text>().color = new Color(0, 126, 255); // default color
    }
}

My question is: how to import this script into button?
I tried to drag&drop script just into button inspector but it doesn’t work.

Hello

Are the Buttons gameobjects? If yes Then you can click add component or drag into the insepector.

Christoph

Partially you’re right.
I had to drag onto Text inspector only , dragging into Button inspector didn’t work…