0

I hope my question is not duplicate. I have two buttons. I want to do an operation after two buttons are clicked on the page . The time the buttons are pressed may be different. Please help me with an example.

Programmer
  • 121,791
  • 22
  • 236
  • 328
ali karimifard
  • 115
  • 2
  • 13
  • 2
    Create two bools. Each enabled by one of the buttons. If two bools are true. Do your operation. – Thalthanas Sep 28 '17 at 09:07
  • The title makes it a [duplicate](https://stackoverflow.com/questions/41391708) but I will change it to reflect pressing two buttons. Look at the #2 from the link I provided above then use EmreE's suggestion to get what you want – Programmer Sep 28 '17 at 09:11
  • Register your buttons to an observable list, and subscribe the class which must monitor the clicks to the observable. – Transcendent Sep 28 '17 at 09:17
  • @EmreE ok.....but i would run a method .......where i check tow bool variable is true. – ali karimifard Sep 28 '17 at 09:18
  • Do you know how to use UI Buttons OnClick method? If not [Check Here](https://www.youtube.com/watch?v=TYzdhiRiKd0) a video from youtube. Shows how to assign scripts to buttons. – Thalthanas Sep 28 '17 at 09:23
  • @EmreE Yes ..... but should not have a supervisory process like coroutine the one that is running and wait until the buttons are clicked ......please help – ali karimifard Sep 28 '17 at 09:33

1 Answers1

3

Have a simple boolean variable for each button you set to true when the button is pressed. You can then check if both boolean variable are true in the Update function.

Attach to an empty GameObject and drag both buttons to the slots:

public Button button1;
public Button button2;

bool button1Pressed = false;
bool button2Pressed = false;

void OnEnable()
{
    //Register Button Events
    button1.onClick.AddListener(() => buttonCallBack(button1));
    button2.onClick.AddListener(() => buttonCallBack(button2));

}

private void buttonCallBack(Button buttonPressed)
{
    if (buttonPressed == button1)
    {
        //Your code for button 1
        Debug.Log("Clicked: " + button1.name);
        button1Pressed = true;
    }

    if (buttonPressed == button2)
    {
        //Your code for button 2
        Debug.Log("Clicked: " + button2.name);
        button2Pressed = true;
    }
}

void Update()
{
    //Check if both buttons have been pressed
    if (button1Pressed && button2Pressed)
    {
        //Do something
        Debug.Log("BOTH BUTTONS HAVE BEEN PRESSED");

        //Set both to false
        button1Pressed = false;
        button2Pressed = false;
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • yeah yeah.......i like you brother – ali karimifard Sep 28 '17 at 09:37
  • Thank EmreE as his/her first [comment](https://stackoverflow.com/questions/46465197/detect-when-multiple-buttons-are-pressed/46465685#comment79885267_46465197) is actually what I put as an answer after I noticed you are still struggling to do this. Happy coding! – Programmer Sep 28 '17 at 09:39