Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by garrei · Oct 25, 2017 at 08:42 PM · arraysxmllistsxmlserializerquiz

How to load specific data from XMLArray

Hi so my goal here is to make a little quiz game thing and try and load the question data from an external xml file. (I get how to do the external thing, just haven't done it yet)... Right now I'm having a problem with getting the options for a certain question to load into the 4 UI buttons. My XML Parser is working, I can import the data of the question itself and display the current question count and also I can get the correct answer to print in console, but I've simply hit a brick wall about where to go from here. The is my first time working with XML data so sorry if I'm super noobish with it! Attached is my test Question Data XML File. alt text

Here is my QuestionContainer.cs:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Xml.Serialization;
 using System.Xml;
 using System.IO;
 
 [XmlRoot("WalkThePlank")]
 public class QuestionContainer { 
 
     [XmlArray("Questions")]
     [XmlArrayItem("Question")]
     public List<Question> questions = new List<Question> ();
 
     public static QuestionContainer Load(string path)
     {
         TextAsset _xml = Resources.Load<TextAsset>(path);
 
         XmlSerializer serializer = new XmlSerializer (typeof(QuestionContainer));
         StringReader reader = new StringReader(_xml.text);
         QuestionContainer questions = serializer.Deserialize (reader) as QuestionContainer;
         reader.Close ();
         return questions;
     }
 }
 

Here is Question.cs:

 using System.Collections;
 using System.Xml;
 using System.Xml.Serialization;
 using UnityEngine;
 
 public class Question {
 
     [XmlAttribute("number")]
     public int number;
 
     [XmlElement("Piratequestion")]
     public string piratequestion;
 
     [XmlElement("Option")]
     public string Option;
 }
 

Here is QuestionUI.cs:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class QuestionUI : MonoBehaviour {
 
     public const string path = "questionData";
     public int currentQuestion;
     public Text questionText;
     public Text button1Text;
     public Text button2Text;
     public Text button3Text;
     public Text button4Text;
     public Text questionsCountText;
     public int totalQuestions;
     public int remainingQuestions;
 
     // Use this for initialization
     void Start () {
         currentQuestion = 1;
 
         QuestionContainer qc = QuestionContainer.Load(path);
         totalQuestions = qc.questions.Count;
         remainingQuestions = totalQuestions;
 
         foreach (Question question in qc.questions) 
         {
             if (question.number == currentQuestion) 
             {
                 questionText.text = question.piratequestion;
                 foreach (Question option in qc.questions) 
                 {
                     print (question.Option);
                 }
             }
         }
     }
     
     // Update is called once per frame
     void Update () 
     {
         questionsCountText.text = "Question " + currentQuestion.ToString () + "/" + totalQuestions.ToString ();
     }
 }

I'm doing most of my work in QuestionUI so its a partially completed script, no idea what to do now to get it to work... please help, I'm a desperate man <3

untitled.png (24.1 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AcidFz · Aug 30, 2020 at 10:05 AM 0
Share

Was your question answered, though this posted long time ago,

Was also thinking about almost the same. Im no coder and dont know c#, but my idead was if there is no internet or offline, use the current xml file if online fetch the xml from the server.

I got a free True or False asset and the xml works great, just want to add that line.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random XML Questions Using Fisher-Yates Shuffle 0 Answers

Randomize text position for 2D Quiz C# 0 Answers

How can you find the index or a variable associated to said index, from a script run from that instance of the game object? 0 Answers

List resulting in out of range 0 Answers

xml file read as MonsterCollection.cs 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges