Simple Button Pressed Question?
Hi! I have a quiz game with 5 possible answers for which I have an array of 5 buttons. When one button is pressed, I want the button number to be checked against the answer number, so that either "correct" or "incorrect" comes up. How do I get the pressed button to return its integer? I tried using onClick listener but either it is the wrong thing to use or I am not doing it right... Any help would be appreciated.
This is what I have at the moment:
public void GetButtonPressed()
{
}
public void checkAnswer()
{
{
if (allowSelection)
{
if (buttonPressed == correctAnswerNum)
{
print("correct");
numberOfCorrectAnswers++;
feedbackText.GetComponent<Text>().text = "Correct!";
feedbackText.GetComponent<Text>().color = Color.white;
}
else
{
print("incorrect");
feedbackText.GetComponent<Text>().text = "Incorrect!";
feedbackText.GetComponent<Text>().color = Color.white;
}
What do you mean by "How do I get the pressed button to return its integer?"
Hi there! Thanks for your reply. What I mean is that I have 5 buttons in the array: 0,1,2,3,4. When I press one of the buttons, I want it to tell me which number I pressed.:)
i can tell you an easy way out (it might help you). Just name the buttons 1,2,3,4 and so on. and in the onClick event just return the name of the button you clicked, store that and play around with it as you wish.
Answer by Toon_Werawat · Mar 22, 2016 at 08:07 PM
May be you need a little modify to checkAnswer(); Here
public void checkAnswer(int buttonPressed)
Fabulous! That worked perfectly. Thank you so much!:)
Your answer
Follow this Question
Related Questions
How to reseting int ?? 1 Answer
How to write and return a call for AddListener 1 Answer
Add listenner to button in loop error 0 Answers