- Home /
How to add the score in a quiz game with text input field and a button.
Hey guys, I have manage to make these codes from the Quiz Game Live tutorial to make its questions randomize and now my problem is I have a text input field and a button, I need to add a score if the input answer and the answer on the questions answer class is the same but no score has been added and it only changes to the next question. Here is the code for my game controller:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.SceneManagement; using System.Linq;
public class idGameController : MonoBehaviour {
public TMP_Text questionText;
public TMP_Text scoreText;
public TMP_Text resultText;
public TMP_Text answerInputText;
public GameObject questionDisplay;
public GameObject resultDisplay;
private idAnswerData IDanswerData;
private idDataController dController;
private idRoundData currentRound;
private idQuestionData[] questionPool;
private static List<idQuestionData> unansweredQuestions;
private idQuestionData currentQuestion;
bool isRoundActive;
private int qIndex;
private int playerScore;
void Start () {
dController = FindObjectOfType<idDataController> ();
currentRound = dController.ID_getCurrentRoundData ();
questionPool = currentRound.questions;
playerScore = 0;
qIndex = 0;
if (unansweredQuestions == null || unansweredQuestions.Count == 0) {
unansweredQuestions = questionPool.ToList<idQuestionData> ();
}
ShowQuestion ();
//SetCurrentQuestion ();
isRoundActive = true;
}
private void ShowQuestion()
{
qIndex = Random.Range (0, unansweredQuestions.Count);
currentQuestion = unansweredQuestions [qIndex];
// QuestionData questionData = questionPool [qIndex];
questionText.text = currentQuestion.questionText;
unansweredQuestions.RemoveAt (qIndex);
}
public void AnswerButtonClicked()
{
if (answerInputText.text == currentQuestion.answer.answerText ) {
playerScore += currentRound.points;
scoreText.text = "Score: " + playerScore.ToString ();
}
if (playerScore <= 5) {
resultText.text = "Result: GOOD!";
} else if (playerScore <= 9) {
resultText.text = "Result: VERY GOOD!";
} else if (playerScore == 10) {
resultText.text = "Result: PERFECT!";
} else if (playerScore == 0) {
resultText.text = "Result: REVIEW!";
}
if (questionPool.Length > qIndex + 1) {
qIndex++;
ShowQuestion ();
if (unansweredQuestions.Count == 0)
EndRound ();
}
}
public void EndRound()
{
isRoundActive = false;
questionDisplay.SetActive (false);
resultDisplay.SetActive (true);
}
public void ReturnToMenu()
{
SceneManager.LoadScene ("Main");
}
}
I changed the if statement condition on the AnswerButtonClicked method because the one in the tutorial is a boolean to check if the answer is correct which is, the one that I need is for the user to type his answer then click the button to confirm the answer.
Hope you help me guys here, thanks!
Is the score being incremented at all? Verify if the score int is increasing and the scoreText just isn't being set properly.
Yes, it is. This is the statement that should happen if I press the button.
if (answerInputText.text == currentQuestion.answer.answerText ) { playerScore += currentRound.points; scoreText.text = "Score: " + playerScore.ToString ();
The problem is I don't have any idea how I can access the answerText that happens to be in a idAnswer class to be the same as what the user types on the input text field to have score if its the same.
Your answer
![](https://koobas.hobune.stream/wayback/20220612191330im_/https://answers.unity.com/themes/thub/images/avi.jpg)