detect the button i press and == it with another var

Hi all,
im new in unity cant understand how to work with UI buttons.
i need to take the value from button i press and check if it similar with another var.

it should be like: if (PressedButton=ans){Do something}

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Buttons : MonoBehaviour {

	public GameObject ui_b1;
	public GameObject ui_b2;
	public GameObject ui_b3;
	public GameObject ui_b4;

	Text b1;
	Text b2;
	Text b3;
	Text b4;

	public int B1;
	public int B2;
	public int B3;
	public int B4;

	public static int Ans = 123;

		void Start () {

		int temp = Random.Range (1,5);

		b1 = ui_b1.GetComponent<Text>();
		b2 = ui_b2.GetComponent<Text>();
		b3 = ui_b3.GetComponent<Text>();
		b4 = ui_b4.GetComponent<Text>();

		if (temp == 1) {
			B1 = Ans;
			B2 = Random.Range (0, 101);
			B3 = Random.Range (0, 101);
			B4 = Random.Range (0, 101);
		} else if (temp == 2) {
			B1 = Random.Range (0, 101);
			B2 = Ans;
			B3 = Random.Range (0, 101);
			B4 = Random.Range (0, 101);
		} else if (temp == 3) {
			B1 = Random.Range (0, 101);
			B2 = Random.Range (0, 101);
			B3 = Ans;
			B4 = Random.Range (0, 101);
		} else {
			B1 = Random.Range (0, 101);
			B2 = Random.Range (0, 101);
			B3 = Random.Range (0, 101);
			B4 = Ans;
		}

	}
	
	void Update () {

		b1.text = ""+B1;
		b2.text = ""+B2;
		b3.text = ""+B3;
		b4.text = ""+B4;

	}
}

Hi

You can make a function lets call it **CompareButtonText( int buttonNum )** that takes an int as a parameter for example , then on the UI Button **in the inspector** you can point **each button** to this function and set the buttonNum parameter for **each button**.

For example **button 1 set it to 0 , button 2 set it to 1 and so on** , for the way i create this function is important that you set the buttons to start at **zero** because i use arrays and they start at zero so the first button has to be set to zero in this particular example.

Something like this should do it you dont even need to use the Update Loop for this.

public string _stringToCompareTo = "MyString" ;

// All Button Texts ( Set In Inspector)
public UnityEngine.UI.Text[] _allButtonsTexts ;

// Call this function from each UI Button and set the parameter to be the same as the corresponding Button Text Index in the array

void CompareButtonText ( int buttonNum ) {
    if ( _allButtonsTexts[ _buttonNum ].text == _stringToCompareTo ){
        //DoSomething();
    }
} 

This is an example it can be done in different ways, i like to use arrays and get what i need by the index number.

Hope it helps i use UnityScript i tried to convert to c# i hope its OK let me know if you get stuck.