I’m trying to collect data from an xml file stored on a web server. So far i’ve had no trouble actually bringing the data into unity by means of the WWW class.
The problem that i’m having is getting unity to keep updating the information so that I can change the xml file on the web server and unity will show these changes. At the moment unity only shows any changes made to the xml file after the program has been stopped and started again.
using System.IO;
using System.Xml;
using System.Collections;
using UnityEngine;
public class Load_Xml2 : MonoBehaviour
{
public int [] imported;
public XmlNode node2;
private float lastime;
private WWW www;
private XmlDocument xmlDocument;
public string xmlFilePath = "/data.xml";
void Awake()
{
lastime = 0.0f;
xmlDocument = new XmlDocument();
}
void Update()
{
if ((Time.fixedTime - lastime) >= 10)
{
lastime = Time.fixedTime;
www = new WWW ("http://10.15.187.27/data2.xml");
print ([url]www.isDone[/url]);
xmlDocument.LoadXml([url]www.data[/url]);
print (lastime);
// print (Time.fixedTime);
// StreamReader xmlStream = new StreamReader( xmlFilePath );
// string xmlText = xmlStream.ReadToEnd();
if ([url]www.isDone[/url])
{
if( xmlDocument != null )
{
// get element by tag //
node2 = xmlDocument.GetElementsByTagName("department1").Item( 0 );
imported[0] = System.Convert.ToInt16(node2.InnerText);
node2 = xmlDocument.GetElementsByTagName("department2").Item( 0 );
imported[1] = System.Convert.ToInt16(node2.InnerText);
node2 = xmlDocument.GetElementsByTagName("department3").Item( 0 );
imported[2] = System.Convert.ToInt16(node2.InnerText);
node2 = xmlDocument.GetElementsByTagName("department4").Item( 0 );
imported[3] = System.Convert.ToInt16(node2.InnerText);
node2 = xmlDocument.GetElementsByTagName( "department5" ).Item( 0 );
imported[4] = System.Convert.ToInt16(node2.InnerText);
node2 = xmlDocument.GetElementsByTagName( "department6" ).Item( 0 );
imported[5] = System.Convert.ToInt16(node2.InnerText);
xmlDocument = null;
}
else imported[0] = 999;
}
}
}
}
When i run the program it displays the contents of the xml file on the screen after 10 seconds. After another 10 seconds it throws the error - “Object reference not set to an instance of an object”. I’m not sure if its an xml related issue that i’m having or if there is something that i’m missing about the WWW class :? [/code]