Hi Forum,
I’m experiencing strange behaviour with a UnityWebRequest. When I start the game the first time after Unity launched, it returns the expected string, also when called several times - when it’s still the first gamestart. When the game is stopped and restarted, the string is empty.
What’s wrong? Or is this a reproducable bug? (Version 2018.4.1f1)
Any help appreciated, I attached the code:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class GameServer : MonoBehaviour{
protected string url = "http://words.lkl/whatword-server/";
protected string noCacheUrl;
protected WWWForm postData;
protected string result = "";
protected ServerCallback callback;
public WWWForm PostData { set { this.postData = value; } }
public string Url { set { this.url = value; } }
public string Result { get { return result; } }
public ServerCallback Callback { set { callback = value; } }
public void Start()
{
postData = new WWWForm();
}
public void StartRequest(WWWForm newPostData = null, bool json = false)
{
noCacheUrl = url + "?t=" + GL.getUTCTime();
if (null != newPostData) PostData = newPostData;
StartCoroutine("PostRequest");
}
public IEnumerator PostRequest()
{
UnityWebRequest webRequest = UnityWebRequest.Post(noCacheUrl, this.postData);
{
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
string[] pages = this.url.Split('/');
int page = pages.Length - 1;
if (webRequest.isNetworkError)
{
Debug.Log(pages[page] + ": Error: " + webRequest.error);
callback("");
}
else
{
// receive Datastring
this.result = webRequest.downloadHandler.text;
// AT this point the string is empty in case of error, so no need to check in the callback
callback(this.result);
}
}
}
public delegate void ServerCallback(string wwwResult);
}