Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Looking to dynamically change the value in the button OnClick and slider On Value Changed example 1 example 2.

If I had to guess on how this was done, get a reference to the button/gui element. Next get the Button script component and access that list of function calls. However I'm not sure how to modify that list. Anyone have any ideas?

More Details that might help:

  • The function being called is apart of a custom script component
  • Using the 'dynamic' variable option for the sliders
share|improve this question

1 Answer 1

up vote 4 down vote accepted

There's a super simple way to change events:

Button button = buttonObject.GetComponent<Button>();
//Remove the existing events
button.onClick.RemoveAllListeners();
//Add your new event
button.onClick.AddListener(delegate {
            Debug.Log("Button " + button.name + " has been clicked!");
    });

Additionally, if you need more than just the events provided by default, I'd suggest instead attaching a EventTrigger to your game object. This gives us access to the BaseEventData object returned from the event, telling us stuff like the object that created the event. Then you can do something like:

//Create an event delegate that will be used for creating methods that respond to events
public delegate void EventDelegate(UnityEngine.EventSystems.BaseEventData baseEvent);

Then we can create a method for handling events, the signature must match that of our delegate. So, it needs to return void and accept BaseEventData as its first and only parameter:

public void DropEventMethod(UnityEngine.EventSystems.BaseEventData baseEvent) {
    Debug.Log(baseEvent.selectedObject.name + " triggered an event!");
    //baseEvent.selectedObject is the GameObject that triggered the event,
    // so we can access its components, destroy it, or do whatever.
}

Finally, to dynamically add the event:

//Get the event trigger attached to the UI object
EventTrigger eventTrigger = buttonObject.GetComponent<EventTrigger>();

//Create a new entry. This entry will describe the kind of event we're looking for
// and how to respond to it
EventTrigger.Entry entry = new EventTrigger.Entry();

//This event will respond to a drop event
entry.eventID = EventTriggerType.Drop;

//Create a new trigger to hold our callback methods
entry.callback = new EventTrigger.TriggerEvent();

//Create a new UnityAction, it contains our DropEventMethod delegate to respond to events
UnityEngine.Events.UnityAction<BaseEventData> callback =
    new UnityEngine.Events.UnityAction<BaseEventData>(DropEventMethod);

//Add our callback to the listeners
entry.callback.AddListener(callback);

//Add the EventTrigger entry to the event trigger component
    eventTrigger.delegates.Add(entry);
share|improve this answer
    
Thanks Byte56 your answer is very helpfull. But somehow there seems to be a Unity bug with EventTriggerType.PointerUp. While EventTriggerType.PointerDown works flawless, EventTriggerType.PointerDown has baseEvent.selectedObject null the first time you click the Object. After the first time clicking it´s not null anymore and works just fine. –  Kreshi Oct 2 at 13:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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