Having issues with WWW and waiting for response.

Hey All,

I am struggling with doing a simple web request and waiting for the response. My game just locks up and I have to kill it via the task manager. I am very new to Unity but am very familiar with .NET so i think thats helping the frustration which is blinding me to what im assuming is a very simple solution.

Here is what i have so far (after several failed attempts so please excuse my messy code):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WebFunctions : MonoBehaviour
{

    private bool hasResponse;
    private WWW responseObject; 
	
	private string currentURI;
	private WWWForm currentForm;
	
	IEnumerator SendGetRequest()
	{
		hasResponse = false;
		WWW w = new WWW(currentURI);
		
		yield return w;
		
		responseObject = w;
		hasResponse = true;
	}
		
	IEnumerator SendPostRequest()
	{
		hasResponse = false;
		WWW w = new WWW(currentURI, currentForm);
			
		yield return w;
				
		responseObject = w;
		hasResponse = true;
	}
	
    public WWW Post(string URI, WWWForm form)
    {
        WaitForSeconds n;
        currentURI = URI;
        currentForm = form;
		StartCoroutine(SendPostRequest());
		
        while (!hasResponse)
            n = new WaitForSeconds(0.1f);

        return responseObject;
    }

    public WWW Get(string URI)
    {
		WaitForSeconds n;
		currentURI = URI;
		StartCoroutine(SendGetRequest());
        while (!hasResponse)
            n = new WaitForSeconds(0.1f);

        return responseObject;
    }
}

Any and all help would be appreciated.

EDIT:
With the help of Chris i managed to get it to work the way i wanted, here is the solution:

using UnityEngine;
using System.Collections.Generic;

public class WebFunctions
{
	public static WWW Get(string url)
	{
		
		WWW www = new WWW (url);

        WaitForSeconds w;
        while (!www.isDone)
            w = new WaitForSeconds(0.1f);

		return www; 
	}
	
	public static WWW Post(string url, Dictionary<string,string> post)
	{
		WWWForm form = new WWWForm();
		foreach(var pair in post)
            form.AddField(pair.Key, pair.Value);

		WWW www = new WWW(url, form);

        WaitForSeconds w;
        while (!www.isDone)
            w = new WaitForSeconds(0.1f);

		return www; 
	}
	
}

And you simply call it like this from your code & it will wait for a response before executing your next line of code:

var response = WebFunctions.Get("http://www.google.com");
// right here you can access response.text or any other property
SomeRandomTextComponent.text = response.text;

This is what i did in my game

IEnumerator ressponceCoroutine()
{
    WWW w = new WWW("Your Api URl");
    yield return new WaitUntil(() => w.bytesDownloaded > 0);
    TextComponent.text = w.text;
}
// Call in Start Method
void Start () 
{
    StartCoroutine(ressponceCoroutine());
}

Hi,

did you already read the docs about it?

There it says:

You can inspect the isDone property to
see if the download has completed or
yield the download object to
automatically wait until it is
(without blocking the rest of the
game).

You can give your elements, ui’s or whatever you using, a default content and after the yield returns the www response set that New content directly in the coroutine.