# How can i set OnClick equal to UnityEvent

## The Problem

I'm making a dialogue system for a small 2d rpg. I Have a Dialogue class with two lists for pages and responses. The response class has a UnityEvent so i can use the ui in the inspector to set methods like a button. Then in a DialogueManager class i have a Show() method which makes a button for every response in the list from Dialogue. The problem is when i try to set the created button's OnClick() event to the UnityEvent in the response.

## The Question

So the question is how can i set the button's OnClick() to the methods in the UnityEvent from the response?

• Presumably you read the docs on the button's onClick UnityEvent and tried using the AddListener method based on the example shown there? Show us what you tried and where specifically you ran into trouble. – DMGregory Feb 27 '19 at 23:07
• I can't use AddListner because i can't find a way get what methods are in the response's UnityEvent. – ThePumkinMelon Feb 28 '19 at 1:44

## 1 Answer

Just make a method that invokes your event:

public class DialogueItem : MonoBehaviour {

public UnityEvent response;

public void FireResponses() {
response.Invoke();
}

public void WireUpResponses(Button button) {
button.onClick.AddListener(FireResponses);
}
}