I hope my question is not duplicate; I have a lot of Gameobjects
in unity.
i need to add onclicklistener
in runtime to a game object with script...
please help.
Use UnityEvent. AddListener to extend the onClick click events. The added UnityAction is called when the Button press is released. A Button can have multiple listeners. As an example, in the script example below, btn3 can have TaskOnClick added as a second listener call.
In order to create an empty game object, the only thing that you need to do is to create an instance of GameObject using the new keyword. GameObject obj = new GameObject( ); GameObject obj = new GameObject( );
copy = new gameobject(); copy = original; copy.
Here is a code that add an EventTrigger
at runtime:
YourGameObject.AddComponent(typeof(EventTrigger));
EventTrigger trigger = YourGameObject.GetComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
entry.callback.AddListener( (eventData) => { /* Your code here */ });
trigger.triggers.Add(entry);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With