Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trigger a button's onClick from another button

Discussion in 'UGUI & TextMesh Pro' started by CremaGames, Sep 3, 2014.

  1. CremaGames

    CremaGames

    Joined:
    Mar 5, 2014
    Posts:
    59
    Hi,

    Is it possible to click a button automatically when another button is clicked by the user? I know it sounds weird :)

    I have this:



    The "+" button is working fine right now, it has a generic animation used in all the buttons in my project and when clicked goes to a function in my UIManager. Now I want to make the pill-background also clickable, and when this is clicked it should propagate the click on the "+" button so the animation will trigger and the function too.

    I could make the whole thing the button and make a new animation for this and only animate the "+" image but I would like to keep the button animation generic.

    Cheers
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    I think you can do this using ExecuteEvents.Execute(). For example:

    Code (csharp):
    1. ExecuteEvents.Execute(plusBtn.gameObject, newBaseEventData(__yourEventSystem__), ExecuteEvents.submitHandler);
    Should work I think. =)
     
  3. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    Almost! I found that ExecuteEvents.submitHandler only causes the animation to play, but won't trigger any attached scripts.
     
  4. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Oh, sorry, this essentially just calls `OnSubmit()` on the object directly, which is what I just assumed you wanted! If you want to go through the EventTrigger system, you can just call the appropriate delegate (UnityAction) there, like so (I hope; this is untested):

    Code (csharp):
    1. plusBtn.GetComponent<EventTrigger>().OnSelect(newBaseEventData(__yourEventSystem__));
     
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    It was the other guy who wanted it - I just thought it was a nice idea for something I'm doing :)
     
  6. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    Oh, ha, I see now! Oh well, more answers in this topic for future people to find! \0/ Did you get it to work properly btw? =)
     
  7. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    I got the doubling up to work fine, and left it at that :)
     
    Senshi likes this.
  8. CremaGames

    CremaGames

    Joined:
    Mar 5, 2014
    Posts:
    59
    This works perfect, thanks!
     
    Senshi likes this.
  9. webster

    webster

    Joined:
    Jun 8, 2013
    Posts:
    21
    If you want to have one button trigger another, and not write any script, use this component:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class InvokeButton : MonoBehaviour
    6. {
    7.  
    8.     public Button button;
    9.  
    10.     void Awake ()
    11.     {
    12.         if (button == null)
    13.             button = GetComponent<Button>();
    14.     }
    15.  
    16.     public void Invoke()
    17.     {
    18.         if (button != null && button.onClick != null)
    19.             button.onClick.Invoke();
    20.     }
    21. }
    If the object you place this on has a Button already, well now you have InvokeButton.Invoke which you can put in any EventTrigger (or similar) right in the Inspector. You can put this on any object and drag your target Button onto it for similar functionality.

    I use this in conjunction with a 'gaze detector' to fire a Button after a couple of seconds of gazing-at (you know, for VR)
     
    Proto-G and warrenLD like this.
  10. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033


    It's been nearly three years. It's decomposed now.
     
  11. warrenLD

    warrenLD

    Joined:
    Jul 2, 2019
    Posts:
    9
    Thank you webster for necroing this thread back in 2017 because you saved my ass in 2020
     
  12. tallchristos

    tallchristos

    Joined:
    Nov 30, 2019
    Posts:
    2
    Thank you Webster. This code is really helpfull.
     
  13. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    Can you modify this to use in an array of buttons that are created during RunTime? I'm trying to modify a Unity Asset Template to work with existing VRTK radial menu and figure it would be easiest to use your method. But the buttons are populated at RunTime, and I;m novice with c# scripting, so am not sure how to go about this. Thanks :)
     
  14. amitaman

    amitaman

    Joined:
    Nov 18, 2021
    Posts:
    1
    Webster Can pls tell me where we have use the button object , null object etc ..... plss
    :(