Question by
IvayloDev · Jan 10, 2016 at 03:31 PM ·
randomxmlxmlserializerquiz
Random XML Questions Using Fisher-Yates Shuffle
I am making a Quiz/Trivia Game and have questions stored in XML file and it gets a random question. Everything works fine, but I want to stay random but not show the same question until every question has been shown.
public struct Question {
public string questionText;
public string answerA;
public string answerB;
public string answerC;
public string answerD;
public int correctAnswerID;
}
[XmlRoot("QuestionsRoot")]
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 I use this to get a random question:
questionData = QuestionData.LoadFromText(questionDataXMLFile.text)
q = Random.Range(0, questionData.questions.Count);
currentQuestion = questionData.questions[q];
Comment
Your answer
Follow this Question
Related Questions
How to load specific data from XMLArray 0 Answers
Unity game doesn't work after binding 0 Answers
How to make questions random, and include from a folder not inside the unity editor 0 Answers
How to save Slider fill area in Xml? 0 Answers
Loading xml on android . Tried and it Works in windows... Help ? 0 Answers