getting data from php using post in other than start method gives problem

Hi,

I am using post method to get the data from PHP. I am using a separate class for this. Assigning URL to WWW in other public method(not in Start method) says that WWW instance is not there(null object reference) when called from othe class.If using same in start method,when this class is initiated from another class, the start method doesn’t gets executed at all. I am not getting where the problem lies in.Can anyone please help me with this ASAP.

public class GetDudeData : MonoBehaviour {

	WWW www;
	public string dudename;
	
	// Use this for initialization
	void Start () {
		
		print("GetDudeData");
		string url = "http://3cubetech.com/dudeapp/dudes_php1/getAllDudes.php";
		
		//Post
		WWWForm form = new WWWForm();
        form.AddField("udid", "7bc92beb9dd8cbad7d6d7a70e1cbdcfe");
        form.AddField("device_type", "iOS");
        www = new WWW(url, form);	
		StartCoroutine(WaitForRequest(www)); 
	}
	
	public void getData()
	{	
		
	}
	// Update is called once per frame
	void Update () {
	
	}
	
	
	IEnumerator WaitForRequest(WWW www)
	{		
		//print(www);
		yield return www;
		if(www.error == null)
		{
			//Debug.Log(www.data);
			readXML();
		}
		else 
		{
			Debug.Log(www.error);
		}
	}
	
	private void readXML()
	{
		XmlDocument doc = new XmlDocument ();
		doc.LoadXml(www.data);
		
		ArrayList arr = new ArrayList();	
		arr.Add("BLUE");
		arr.Add("YELLOW");
		arr.Add("MAX");
		arr.Add("RED");
		
		XmlNode root = doc.SelectNodes("/response/qr_codes")[0];
		
		XmlNode nodes = doc.CreateElement("DudeColors");		
	
		foreach(XmlNode list  in root)
		{
			XmlNode name = list.SelectSingleNode("dude_name");
			XmlNode color = list.SelectSingleNode("dude_color");			
			
			if(name.InnerText == dudename )
			{			
				foreach(string dudecolor in arr)
				{
					if(color.InnerText == dudecolor)
					{
						print("equal");
						nodes.AppendChild(list);
						break;
					}					
				}
				break;
			}
			
		}		
		print("dudes data:"+nodes.InnerXml);
		//my changes
	}

Divya

The problem lies in the order operations are carried out.

I’d guess that the initialization of the GetDudeData class is incomplete when the other class tries to use the variable www.

You are only guaranteed that Start() will run before Update() is called for the first time, so after creating/instantiating a gameobject anything done to it before the next frame will (or at least “might”) happen before Start() is executed.

My suggestion is to try using Awake() instead of Start().

Failing that, set up your instantiating script to wait a frame after the instantiation before calling on variables set in Start().