I have 4 buttons, 3 with a random value and one with the the correct answer. I am trying to write a single method to attach to the button which will check to see if the value in the buttons text box is the same as the correct answer.
Currently I have four different methods - one for each button
You will need a game object with a script attached. The script could look like this:
class AnswerChecker extends MonoBehaviour{
private var isCorrect:boolean;
public function correct():void{
this.isCorrect = true;
}
public function wrong():void{
this.isCorrect = false;
}
public function isCorrect():boolean{
return this.isCorrect;
}
}
Then you assign the “correct” function to the button with the correct answer and the “wrong” function to the buttons with the wrong answers.
This way you will be able to acess the answer from other scripts by using GameObject.Find(“your object name here”).GetComponent(AnswerChecker).isCorrect()
An alternative structure would be to have one button for the right answer and three for the wrong answer. Then scramble their positions on screen.
To answer your question you can pass in a Untiy Object as a parameter in your OnClick event. Simply pass in the GameObject of the button. Or an int that represents the button ID.