Question by
DesQuik · Feb 17, 2019 at 11:15 AM ·
nullreferenceexceptionxml
Output from XML. NullReferenceException.
Hello. Pls, someone, help me. So I have a xml file:
And script for him:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Linq;
[XmlRoot("Dialogue")]
public class DialogueManager
{
[XmlElement("Node")]
public Node[] nodes;
public static DialogueManager Load(TextAsset _xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(DialogueManager));
StringReader reader = new StringReader(_xml.text);
DialogueManager dialogue = serializer.Deserialize(reader) as DialogueManager;
return dialogue;
}
}
[System.Serializable]
public class Node
{
[XmlAttribute("Name")] public string nameText;
[XmlAttribute("CharText")] public string charText;
[XmlElement("Answer")]
public Answer[] answers;
}
[System.Serializable]
public class Answer
{
[XmlAttribute("Text")] public string textAnswer;
[XmlAttribute("ToNode")] public int toNode;
[XmlAttribute("Exit")] public string exit;
}
And output script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public TextAsset textFile;
void Start()
{
DialogueManager dialogue = DialogueManager.Load(textFile);
foreach (Node nd in dialogue.nodes)
{
Debug.Log(nd.nameText);
Debug.Log(nd.charText);
foreach (Answer an in nd.answers)
{
Debug.Log(an.textAnswer);
}
}
}
}
All good, but unity finds the NullReference:
Comment
Your answer
Follow this Question
Related Questions
Problem with random music script 1 Answer
How to create an XML in an absolute path with WWW Class 0 Answers
Unity IAP Help, Cannot Figure Out :( 0 Answers
Best way to store a large ammount of spells ? 1 Answer
List Null Ref 1 Answer