- Home /
How to add score only one time after pressed a button
I'm creating Math app and it has four answers to choose right one. it's question change every 4s. but if player pressed answer button multiple time (ex: 4 times). the thing if it need to do is(ex; add 1 score) happen 4 times. is there any solution for that
Answer by V_0_1_D_ · Feb 26 at 08:48 PM
Declare a bool such as hasPressedRightAnswer that is initially false. Inside AddScore method, first check the bool to be false, then add score if it is.Next line inside method, you should change the bool value to true. Also you should change it back to false exactly when the next question appears. Alternatively, in your script, you can simply uncheck "Interactable" property of the button when the the right answer is pressed. then check it when new question appears.
public class YourClassName
{
private bool hasPressedrightAnswer = false;
private int score;
private void AddScore()
{
if (hasPressedrightAnswer == true)
{
return;
}
++score;
hasPressedrightAnswer = true;
}
// And when the next question appears, do this in some method:
hasPressedrightAnswer = true;
// Alternatively
[SerializeField] Button btn;
private void AddScore()
{
++score;
btn.interactable = false;
}
// And when the next question appears, do this in some method:
btn.interactable = true;
}
Answer by Bhanuka_Dassanayake · Feb 27 at 08:33 AM
@V_0_1_D_ thanks for your reply.
extra knowledge for me,
i have four button and i make for functions(change pressedID) for them to tell what happend when pressed. it's work. all four fuctions are same. just change the ID number. but is there any way to use one function for all 4 buttons. for 4 buttons it's okay, but what if i have 50 buttons. is this the way or have any solution.
Your answer

Follow this Question
Related Questions
Rotating clockwise or counter clockwise using Quaternions and PID 0 Answers
2 ints are not getting added 1 Answer
How can I follow the terrain 1 Answer
What does this formula mean? 1 Answer
How to get vector X distance from point on Y angle? 4 Answers