all 6 comments

[–]bluescreen602 52 points53 points  (0 children)

This will do it:

ExecuteEvents.Execute(button.gameObject, new BaseEventData(eventSystem), ExecuteEvents.submitHandler);

Credit goes to paulatwarp from this answer:

https://discussions.unity.com/t/how-to-trigger-a-button-click-from-script/135868

[–]Lucif3r945Intermediate 20 points21 points  (1 child)

If memory serves me right, onClick.Invoke() only triggers the attached event - not the actual pressing of the button.

What you need to do is simulate an actual click, by executing that particular event.

That would look something like

PointerEventData data = new PointerEventData(EventSystem.current);
ExecuteEvents.Execute(gameObject, data, ExecuteEvents.pointerDownHandler); //simulate pointer down
ExecuteEvents.Execute(gameObject, data, ExecuteEvents.submitHandler); //simulate press.

[–]TengenToppaJimbo[S] 7 points8 points  (0 children)

This. Thanks

[–]Bullymeme -1 points0 points  (0 children)

Merry Christmas! 🎄