Getting Questions and Answers from XML file and applying them to the UI
I made the layout of the game, but the coding is throwing me off. How do I make it so instead of making a new label for every question, make it so that the text of the Label
changes? When the question changes the answer changes as well so that it isn't True-True-True-True for the entire game.
Would it be possible if the questions were made in a XML file?
You should really provide more detail in you question; what is your current code/Unity setup and what are you using for this 'label' - is this a Unity UI element or are you referring to an X$$anonymous$$L element?
Using X$$anonymous$$L to host the questions should be easily achievable. You should have a look at the existing resources within the Unity community on the use of X$$anonymous$$L within Unity; by the sounds of what you want, this should be really simple. I would recommend using the X$$anonymous$$L Document class, check out the $$anonymous$$SDN docs on its usage and existing posts on Unity Answers such as this, to get an idea on how this would be achieved.
I'm not that advanced with C# and Unity. I'm a web developer.
Answer by Statement · Oct 24, 2015 at 11:29 PM
It doesn't matter what source the data is coming from. Load it into Data objects you store in memory. Etc define a class for one question and then keep a list or dictionary of them.
public class Question
{
public string text;
public string[] answers = new string[4];
public int answerId;
}
Keep a list of your questions and the current question
List<Question> questions = new List<Question>();
Question currentQuestion;
And create them somewhere as part of your loading
while (CanCreateQuestionFromXml())
questions.Add(CreateNextQuestionFromXml());
And pick one at random etc..
currentQuestion = questions[Random.Range(0, questions.Count)];
And update your UI...
ui_question.text = currentQuestion.text;
ui_answer1.text = currentQuestion.answers[0];
ui_answer2.text = currentQuestion.answers[1];
ui_answer3.text = currentQuestion.answers[2];
ui_answer4.text = currentQuestion.answers[3];
And respond to button press etc...
public void OnAnswer1()
{
correct = currentQuestion.answerId == 0;
RefreshUI();
}
public void OnAnswer2()
{
correct = currentQuestion.answerId == 1;
RefreshUI();
}
public void OnAnswer3()
{
correct = currentQuestion.answerId == 2;
RefreshUI();
}
And so on...
Alright, so if I am using X$$anonymous$$L for the my data, what would be the structure of the X$$anonymous$$L file?
The above approach uses the Generic Dictionary, check out the linked tutorial to get an idea of how this works.
So to use this you need to parse the X$$anonymous$$L document and store the nodes holding the question strings as the dictionary $$anonymous$$ey (using something like XmlNode.SelectNodes("Questions").InnerText) and then store the nodes holding the answers as the dictionary Value (as detailed in the code provided above).
If you haven't used X$$anonymous$$L before, you might want to get an idea of the basic syntax it uses (check out this W3Schools doc)
Answer by adityakin9 · Apr 07, 2018 at 07:46 AM
https://welookups.com/xml/xml_syntax.html
welookups XML Tutorial XML Syntax Rules « Previous Next Chapter »
The syntax rules of XML are very simple and logical. The rules are easy to learn, and easy to use. XML Documents Must Have a Root Element
XML documents must contain one root element that is the parent of all other elements:
<subchild>.....</subchild>
,use this site https://welookups.com/ HTML Tutorials « HTML Home Next Chapter HTML »
HTML
HTML is very important of web page you can create your web pages.
This tutorials teaches you everything about HTML and HTML5.
It's is very simple
Your answer
![](https://koobas.hobune.stream/wayback/20220612042616im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Button.onClick running too many times 0 Answers
Detect if UI button clicked 1 Answer
NullReferenceException with XML Deserializer 0 Answers
Can't access functions on my UI button 1 Answer
GUI Scripting Hierarchy 0 Answers