Scale UI Button With Script

I’m trying to do something super simple, but I can’t figure out how to do it.

I’m trying to scale my button up to a slightly larger size when hovered over, a classic button element.

Here’s what I have:

private Vector3 scaleChange; 

public void ScaleChange()
{
    scaleChange = new Vector3(1.1f, 1.1f, 1.1f);
}

I know this is possible, because I’ve seen this in many Unity games, but for the life of me, I can’t figure out how to do it.

All help is appreciated :slightly_smiling_face:

1 Like

You can make things easier by avoiding to hard code the animations using an Event Trigger which is not a begginer friendly topic. You can learn more about it here

Since you are a beginner though I suggest to use Unity’s animation system for buttons as you can see down here:

This method allows you to create the Animations using the Animator component. If you edit the Scale instead of the Width or the Height of the object you can use the same Animation for all the buttons.
If you aren’t familiar with the Animator component watch this video

I did mess around with the EventTrigger a few times but I don’t reccomend it to you since the code looks something like this:


    private void AddEventTriggerTo(Transform trigger, UnityEvent OnPointerUp, UnityEvent OnPointerDown)
    {
        EventTrigger eventTrigger = trigger.gameObject.GetComponent<EventTrigger>();

        if (eventTrigger == null) eventTrigger = trigger.gameObject.AddComponent<EventTrigger>();
        else eventTrigger.triggers.RemoveRange(0, eventTrigger.triggers.Count);

        EventTrigger.Entry onPointerUp = new();
        onPointerUp.eventID = EventTriggerType.PointerUp;
        EventTrigger.Entry onPointerDown = new();
        onPointerDown.eventID = EventTriggerType.PointerDown;

        onPointerUp.callback.AddListener((eventdata) =>
        {
            OnPointerUp?.Invoke();
        });
        onPointerDown.callback.AddListener((eventdata) =>
        {
            OnPointerDown?.Invoke();
        });
        eventTrigger.triggers.Add(onPointerUp);
        eventTrigger.triggers.Add(onPointerDown);
    }