Maybe it's a repeated subject in the community but other answers didn't solved my question...
I tried as the tutorial: http://answers.unity3d.com/questions/11021/how-can-i-send-and-receive-data-to-and-from-a-url.html and worked fine in start method but when I try:
public Class A
{
// ...CODE...
new B().JSONRequest(jsonString);
}
public Class B
{
public void JSONRequest(string json) {
string url = URL.LOCAL.url;
Hashtable postHeader = new Hashtable();
postHeader.Add("Content-Type", "application/json");
UTF8Encoding encoding = new System.Text.UTF8Encoding();
WWW request = new WWW(url, encoding.GetBytes(requisicaoJSON.ToCharArray()), HashtableToDictionary<string, string>(postHeader));
print("Request: "+request);
StartCoroutine(WaitForRequest(request));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.data);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
}
It gives me in the StartRoutine line: NullReferenceException UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:61
Console prints Request: UnityEngine.WWW so it does not appear to be null. –
gameObject.AddComponent<B>()
- Unity warns you not to try to construct MonoBehaviours by sayingnew B()
- I would not expect Coroutines to work on a floating MonoBehaviour that's not attached to a GameObject. \$\endgroup\$ – DMGregory♦ Nov 25 '16 at 3:02