I am trying to make a quiz game and have just had a question answered about an XML connection error. This has been resolved but now I cannot work out why my text is not displaying the questions and answers from the XML file. I followed this question to make my code for the XML connection and most of the rest of the code. http://answers.unity3d.com/questions/639381/too-much-prefab-for-question-quiz-games.html
Here is my code that I am having trouble with:
[SerializeField]
private TextAsset questionDataXMLFile;
private QuestionData questionData;
private Question currentQuestion;
public Text answerAText;
public Text answerBText;
public Text answerCText;
public Text question;
void Start(){
questionData = QuestionData.LoadFromText (questionDataXMLFile.text);
}
public void SetNewQuestion(){
int q = Random.Range (0, questionData.questions.Count - 1);
currentQuestion = questionData.questions [q];
question.text = currentQuestion.questionText;
answerAText.text = currentQuestion.answerA;
answerBText.text = currentQuestion.answerB;
answerCText.text = currentQuestion.answerC;
}
public bool CorrectAnswerSelected(int selectedAnswerID){
return selectedAnswerID == currentQuestion.correctAnswerID;
}
public void isACorrect(){
if (currentQuestion.correctAnswerID == 0){
//user got it right!
}
else {
//user got it wrong!
}
}
public void isBCorrect(){
if (currentQuestion.correctAnswerID == 1){
//user got it right!
}
else {
//user got it wrong!
}
}
public void isCCorrect(){
if (currentQuestion.correctAnswerID == 2){
//user got it right!
}
else {
//user got it wrong!
}
}
I think the problem is with the:
question.text = currentQuestion.questionText;
and the same with setting the answer texts. On the other tutorial, it says to use code such as: // add code here to set text values of your Question GameObject // e.g. GetComponent().Text = currentQuestion.questionText; }
But I'm not sure how to do this and what values to use etc.
Here is the c# file where I access the XML file:
[XmlRoot("Questions")]
public class QuestionData {
[XmlArray("Questions")]
[XmlArrayItem("Question")]
public List<Question>
questions = new List<Question>();
public static QuestionData LoadFromText(string text){
try{
XmlSerializer serializer = new XmlSerializer(typeof(QuestionData));
return serializer.Deserialize(new StringReader(text)) as QuestionData;
}
catch (Exception e){
UnityEngine.Debug.LogError("Exception loading question data: " + e);
return null;
}
}
}
and here is the XML file with some test questions in:
<Questions>
<Question>
<questionText>What is the capital city of France?</questionText>
<answerA>London</answerA>
<answerB>Paris</answerB>
<answerC>Rome</answerC>
<correctAnswerID>1</correctAnswerID>
</Question>
<Question>
<questionText>What is the capital city of England?</questionText>
<answerA>London</answerA>
<answerB>Paris</answerB>
<answerC>Rome</answerC>
<correctAnswerID>0</correctAnswerID>
</Question>
</Questions>
Ok, I got it to work with these changes:
Change the question struct to this:
Surround the stuff in the xml file with another block like this:
Change
to
EDIT:
One flaw still in there: questionText is Null this way. Need to have a look at that.
EDIT2:
Got that fixed too.
[XMLAttribute("questionText")]
needs to be[XMLElement("questionText")]
. Changed that block above.EDIT3: Some further additions:
Remove the
-1
from Random.Range, integer is inclusive/exclusive.For the button click do this:
Give all the buttons a
OnClick
-event in the inspector and pass the index of the button (matching the answer ids). Example for Button3 here (the script is on theQuestionDisplay
gameobject: