xxxxxxxxxx
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
[SerializeField]
private Button button;
void Start()
{
button.onClick.AddListener(RunThisTask);
}
void RunThisTask(){
//do something
}
}
xxxxxxxxxx
//activates the onClick-Event of the button as if the button was pressed
//you still need to define the function to be called in the Unity editor or via script
private void clickMyButton(Button myButton){
myButton.onClick.Invoke();
}