0
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Feb 27 at 23:07
  • \$\begingroup\$ I can't use AddListner because i can't find a way get what methods are in the response's UnityEvent. \$\endgroup\$ – ThePumkinMelon Feb 28 at 1:44
0
\$\begingroup\$

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);
   }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.