So I'm creating a game that involves lots of different areas that involve pushing buttons to do certain things. Anyway, I'm trying to figure out how to let each button do its own separate thing, like open a door or things like that. I already know how to setup a Raycast between the player's mouse to the button, but now I'm just figuring out how to make each button trigger a separate action. Basically, should I set up a new tag for each button? Or create different scripts, even? I haven't tried anything yet and I need help. Thanks :D

  • \$\begingroup\$ I already know how to setup a Raycast between the player's mouse to the button, but now I'm just figuring out how to make each button trigger a separate action. Are you not using Canvas? Or are we talking about switches in 3D? \$\endgroup\$
    – Sidar
    Jul 26, 2017 at 1:45
  • \$\begingroup\$ @Sidar why, switches in 3d of course! that's why i said like opening doors and such! \$\endgroup\$
    – Daneolog
    Jul 26, 2017 at 1:54
  • \$\begingroup\$ Technically you can also have your character standing next to a door and by pressing a ui button you open it. Just pointing out that it was a bit vague. \$\endgroup\$
    – Sidar
    Jul 26, 2017 at 1:55

1 Answer 1


In unity you can work with UnityEvents. If all of your objects have a single action and two states ( on and off ) you can use two UnityEvents (ie OnButtonPressed or OnButtonEnbaled and OnButtonDisabled) for one script and populate the listeners in the inspector.

This way you can individually setup what functions on what objects must be called. When you press the button you fire the event withing the script. The UnityEvent then calls all the listeners you have set in the inspector or trough script.

Edit: Here I made a script called EventTrigger which can take any object and then call any of their public functions. In this case my target is a game object on which I'm calling the SetActive function.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class EventTrigger : MonoBehaviour {

    public UnityEvent OnActivate;
    public UnityEvent OnDeactivate;

    public void TriggerOn(){
        if(OnActivate != null)

   public void TriggerOff(){
       if(OnDeactivate != null)

Then just call TriggerOn or TriggerOff whenever you have to.

How you connect all the components is up to you. One way would be to request the compontent from the interested object. The EventTrigger works for all gameobjects. Naming is generic so it shouldn't cause any confusion.

Keep in mind that it can only take functions with 0 or 1 parameter. Getter/Setters are also accepted.

  • \$\begingroup\$ Wow!! Can you please give me an example or would that be too hard? Thanks a bunch :D \$\endgroup\$
    – Daneolog
    Jul 26, 2017 at 2:01
  • \$\begingroup\$ I replaced the link, i posted the wrong one. Click the UnityEvents in blue. \$\endgroup\$
    – Sidar
    Jul 26, 2017 at 2:02
  • \$\begingroup\$ reddit.com/r/Unity3D/comments/3dfiuc/tip_generic_trigger_events Here is a simple example \$\endgroup\$
    – Sidar
    Jul 26, 2017 at 2:04
  • \$\begingroup\$ So I know this is a kinda old post but I've been trying this and I've found out there are multiple different states of other objects. For example boxes have locked/not locked, buttons have used/not used, levers have on/off... all that kinda stuff. So how should I do this? \$\endgroup\$
    – Daneolog
    Aug 1, 2017 at 21:40
  • \$\begingroup\$ All of them are on/off -> true/false. So it's just semantics. If you use the Generic UnityEvent (without specifying the types ), in the inspector, you can just drag the object into the event, in the dropdown select the function and you can turn it on or off depending on the on or off event. \$\endgroup\$
    – Sidar
    Aug 2, 2017 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .