help: newbie having trouble with WWW functionality.

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]

Suggestion: strip all that XML related stuff out as that has nothing to do with any issues relating to your ability to pull a text file repeatedly or not. That way your code is simpler and cleaner to look at for the debugging purposes at hand.

Also, your code seems a bit off in that you issue a call to the WWW class print www.isDone and then immediately attempt to use www.data (this is all at the top of your Update function). Generally you should move the WWW usage into a custom function that uses a yield statement. Pardon my use of JavaScript instead of C# but something like this should work:

function Update () {
  PullData();
}

function PullData () {
  var tWWW = WWW("someurl.xml");
  yield tWWW;
  if (tWWW.error != null) {
    // error!
  } else {
    // no error :)
  }
}

That way the yield will cause the script to pause and wait for the WWW operation to complete before proceeding.

Try modifying your code to just pull and dump the text out in some way before worrying about any parsing of that text as XML.

thanks for the fast reply! I’ll give it a go :slight_smile:

I’ve got something working!

var datareceived = " ";
var lastime = 0.0;

function OnGUI () 
{
	

	
   	if ((Time.fixedTime - lastime) >= 5)		
		{
		
		lastime = Time.fixedTime;				
		 PullData();

		}
		
	GUI.Box (Rect(100,300, 200,200), datareceived);

}
	

function PullData () 
{
  var tWWW = WWW("http://10.15.187.27/text.txt");
  yield tWWW;
  if (tWWW.error != null) {
    print ("error");
  } 
  else 
  {
    print ("no error");
  }
  
  datareceived = tWWW.data;
  
}

Initially, unity kept on crashing as soon as i altered and saved the xml file. So I made it only check the xml file every 5 seconds and now it seems to be working fine.

Thanks for pointing me in the right direction it really helped.

I just gotta figure out how to get this method working properly with my other code (in C#).[/code]