During my game, the user inputs multiple answers using the InputField. I would like to store the users answers, right or wrong, and display them when the level is completed side by side with the correct answers.
Example:
User answers: Correct Answers:
Green Blue
Red Red
Red Purple
Blue Blue
White Blue
In reading about lists, arrays and strings I have decided (correct me if I am wrong) a list would be the best way to store the information and call it up to display the inputs at the end.
What would be the best way to do this? My code is already checking the input for the correct answer and behind the scenes adding to the score if the answer is right. When the decided number of questions has been answer or the decided score has been reached, the end game shows.
I can call up the information but I am not sure the best way to store it until I need it.
Thanks in advance.
A List is a good way to store the data, yes. You can initialize the list when the quiz is started, add answers as you go, and it won’t have to be modified for different numbers of questions. Because you have multiple pieces of data, I recommend using a class to store this data, and using List for your list. Here’s some sample code:
public class AnswerData {
public string question;
public string correctAnswer;
public string userAnswer;
public bool WasCorrect() {
return (userAnswer.ToLowerCase() == correctAnswer.ToLowerCase() );
}
}
public List<AnswerData> allAnswers;
public void OnAnswerQuestion(string userInput) {
AnswerData a = new AnswerData();
a.question = currentQuestion;
a.correctAnswer = currentCorrectAnswer;
a.userAnswer = userInput;
if (allAnswers == null) allAnswers = new List<AnswerData>();
allAnswers.Add(a);
ProceedToNextQuestion();
}
So let’s assume you have a script running this quiz. When your user hits enter, you want to take the text they’ve entered, store it along with the correct answer, and add that data to your List. All that should be written into a function that takes a string as a parameter. The script will need access to the current “correct” answer somehow (maybe on the same script), and should bundle that together and add it to the list. It then moves on to the next question, which will involve changing the text on some other UI fields.
Perhaps I am not as comfortable at this as I thought.
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System;
public class gamemanagerexampletest : MonoBehaviour
{
public GameObject tabletInterface;
public int scoreValue;
public scoremanagertest scoreAccess; //This is so the question control can have access to the scoremanager.
[Serializable] // makes this able to be shown in the inspector if all fields are standard types
public class QuestionAnswerSet
{
// this class defines one question, and a list of answers
public string question;
public List<string> answers;
}
// configured in the Inspector
public List<QuestionAnswerSet> questionAnswerList; // list of question-to-answer sets
public InputField userInputField; // the input field the user will type in
private int currentQAIndex; // index of the current question-to-answer set
private QuestionAnswerSet currentQA; // the current question-to-answer set
private void Start()
{
// initialize current question to the first in the list
if(questionAnswerList.Count > 0)
{
currentQA = questionAnswerList[0];
}
}
void Update ()
{
userInputField.text = userInputField.text.ToUpper ();
}
public void NextQuestion()
{
currentQAIndex++;
// if there is no next question, go back to the first question (customize to the behaviour you'd like)
if(questionAnswerList.Count <= currentQAIndex)
{
currentQAIndex = 0;
}
currentQA = questionAnswerList[currentQAIndex];
// display question somehow
string question = currentQA.question;
}
public void Submit()
{
string[] userAnswers = userInputField.text.Split(',');
bool correct = false;
// if any of their answers matches one in the question/answer set, it's correct
foreach(string userAnswer in userAnswers)
{
if(currentQA.answers.Contains(userAnswer))
{
correct = true;
print("User answer: '" + userAnswer + "' is correct!");
break;
}
}
if(correct)
{
// do stuff
tabletInterface.SetActive (false);
scoreAccess.AddScore (scoreValue);
print("Done, Done, Done");
//NextQuestion();
}
else
{
// do other stuff
print("User answers are incorrect");
}
}
public void SetQuestion(int index){
currentQAIndex = index;
currentQA = questionAnswerList[currentQAIndex];
}
//---------------- Idea ---------------------------
public void ActivateSystem ()
{
tabletInterface.SetActive (true);
}
}
If I am understanding you correctly, using my existing code, I would use the current class QuestionAnswerSet since that is already set up to store data. I would keep the public string question and the public List answers since that is the data I need to populate the quiz.
In the public void Submit() I would put in the information to populate the list at the end and display the user answers and the correct answers since I already have them created further up in the code.
Sorry this quiz thing is for some reason blowing my mind, probably because I am working on it on a Friday and now a Monday. Thanks for any other further advice.