I am using a HTTP request to try and access a REST endpoint. I want to display the text I get in a canvas but I can’t seem to get the output of the request to display in the Canvas.
using UnityEngine.Networking;
class Rest_test : MonoBehaviour {
public TextMesh thistext;
// Use this for initialization
void Start () {
StartCoroutine (GetText ());
}
IEnumerator GetText() {
UnityWebRequest www = UnityWebRequest.Get("http://jsonplaceholder.typicode.com/posts/1");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.Send();
if(www.isError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
// Or retrieve results as binary data
//byte[] results = www.downloadHandler.data;
}
}
void Update () {
thistext.text = GetText ();
}
}
I’m pretty new to C# and Unity so i may be missing something very basic.
I get the following error when I add the script to my scene:
Assets/Rest_test.cs(30,18): error CS0029: Cannot implicitly convert type System.Collections.IEnumerator' to
string’