- Home /
Unity 4.6 UI - Trigger OnClick event via script
It seems that in NGUI you can achieve what I want by sending a message:
SendMessage("OnClick");
(from: http://answers.unity3d.com/questions/263605/ngui-simulate-a-button-press-from-script.html)
I would like to do the same but with the new UI system in 4.6. Any ideas?
Did you check out: http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers
Does this help? http://answers.unity3d.com/questions/791274/46-ui-button-press-how.html
Answer by troien · Nov 03, 2014 at 05:01 PM
Although this is not documented very well, currently you can send an event like this. (Not sure if it is the correct way to do this, but it seems to work)
ExecuteEvents.Execute<IPointerClickHandler>(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
Then all Components that are IPointerClickHandler (like buttons) on the given gameobject will receive that event.
If you use this, then the Event will send the event to the first GameObject it can find that has a component with a IPointerClickHandler (Note that this goes only up in the hierarchy, so not to the children)
ExecuteEvents.Execute<IPointerClickHandler>(ExecuteEvents.GetEventHandler<IPointerClickHandler>(gameObject), new PointerEventData(EventSystem.current), ExecuteEvents.pointerClickHandler);
That is basically everything I was able to find out about sending events yourself.
(Unity 4.6.0b20)
Your answer
![](https://koobas.hobune.stream/wayback/20220613170618im_/https://answers.unity.com/themes/thub/images/avi.jpg)