- Home /
Trouble Getting External Data Using WWW
Trying to use WWW to pull data from my webservice and I originally got an error saying "WWW is not ready downloading yet".
So, I figure I have to use "yield return www;", which led to me to using a delegate method. Now I am getting error: "NullReferenceException UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine)".
I'm an experience .net C# developer, but this is seriously giving me trouble. What is the best way to go about something like this? Thanks!
In my ItemCatelog class:
private void RequestItemCatelog()
{
data.GetItemCatelog(RecieveItemCatelog);
}
private void RecieveItemCatelog(CatelogItem[] items)
{
Debug.Log(items[0].ItemName);
}
and in my datahelper class:
public delegate void GetItemCatelogReceiver(CatelogItem[] items);
public void GetItemCatelog(GetItemCatelogReceiver _GetItemCatelogReceiver)
{
StartCoroutine(GetItemCatelogResponse(_GetItemCatelogReceiver));
}
IEnumerator GetItemCatelogResponse(GetItemCatelogReceiver _GetItemCatelogReceiver)
{
WWW www = new WWW(url);
Debug.Log(www.text);
yield return www;
CatelogItem[] items = JsonMapper.ToObject<CatelogItem[]>(www.text);
Debug.Log(www.text);
_GetItemCatelogReceiver(items);
}
Answer by Adamcbrz · Aug 06, 2013 at 03:49 AM
I had a problem when i copied your script because of requesting www.text before yielding to www. Here is the test I did with success and excluding the json list.
using UnityEngine;
using System.Collections;
public class WWWTest : MonoBehaviour
{
public delegate void GetItemCatelogReceiver(string text);
void Start()
{
GetItemCatelog(RecieveItemCatelog);
}
void GetItemCatelog(GetItemCatelogReceiver _GetItemCatelogReceiver)
{
StartCoroutine("GetItemCatelogResponse", _GetItemCatelogReceiver);
}
IEnumerator GetItemCatelogResponse(GetItemCatelogReceiver _GetItemCatelogReceiver)
{
WWW www = new WWW("http://www.google.com");
yield return www;
_GetItemCatelogReceiver(www.text);
}
private void RecieveItemCatelog(string text)
{
Debug.Log(text);
}
}
Unity crashes everytime it reaches "yield return www". Unity 5.3.4
Answer by foxvalleysoccer · Jul 09, 2015 at 03:32 PM
I got it working!
void Awake()
{
isFirstRound = true;
WWW result = GET("http://dev.wisc-online.com/Prototypes/muscle_structure.json");
}
public WWW GET(string url)
{
WWW www = new WWW(url);
StartCoroutine(WaitForWWW(www));
//do nothing untill json is loaded
while (!www.isDone) { }
if (www.error == null || www.error == "")
{
gameData = convertFromJSONToMyClass(www.text);
Debug.Log("gameData = " + gameData);
}
else
{
Debug.Log("WWW error: " + www.error);
}
return www;
}
IEnumerator WaitForWWW(WWW www)
{
yield return www;
}
Answer by grigdog · Aug 06, 2013 at 04:44 AM
The problem was my ItemCatelog class was instantiating the DataHelper class with new(). Old .net habits die hard.
"yield return www" I had put in place to reproduce the "WWW is not ready downloading yet" error and accidently made it into the snippet.
Answer by David-Hernan · May 14, 2015 at 06:55 PM
In my case, my game must wait for the web results before continuing, there is nothing to do while the results arrive, so I programmed it to wait until WWW is ready downloading.
using UnityEngine;
using System.Collections;
public class Principal : MonoBehaviour
{
// This could be one of many methods which require data from web.
public void Browse ()
{
// Make a call to the "GET" method providing the url.
WWW result = GET("https://www.google.com.mx/search?q=UnityException%3A+WWW+is+not+ready+downloading+yet&oq=UnityException%3A+WWW+is+not+ready+downloading+yet&aqs=chrome..69i57j69i58.349j0j9&sourceid=chrome&es_sm=93&ie=UTF-8#safe=off&q=UnityException:+WWW+is+not+ready+downloading+yet");
// Show the output.
Debug.Log(result.text);
}
// The "GET" method is apart because it could be called from many methods.
public WWW GET (string url)
{
// Create the WWW object and provide the url of this web request.
WWW www = new WWW (url);
// Run the web call in the background.
StartCoroutine (WaitForRequest (www));
// Do nothing until the response is complete.
while (!www.isDone) {}
// Deliver the result to the method that called this one.
return www;
}
// Run the web call and deliver the result as it is arriving.
private IEnumerator WaitForRequest (WWW www)
{
yield return www;
}
}