title: Unity UnityWebRequest Coroutine
body: <p>I'm trying to figure out how to properly use UnityWebRequest within a Unity Coroutine and I tried this way but I don't get results:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using System;
  using UnityEngine.Networking;
  
  public class rest : MonoBehaviour
  {
      public Coroutine&lt;T&gt; StartCoroutine&lt;T&gt;(IEnumerator coroutine)
      {
          Coroutine&lt;T&gt; coroutineObj = new Coroutine&lt;T&gt;();
          coroutineObj.coroutine = base.StartCoroutine(coroutineObj.InternalRoutine(coroutine));
          return coroutineObj;
      }
  
      public class Coroutine&lt;T&gt;
      {
          public T Value
          {
              get
              {
                  if (e != null)
                  {
                      throw e;
                  }
                  return returnVal;
              }
          }
          private T returnVal;
          private Exception e;
          public Coroutine coroutine;
  
          public IEnumerator InternalRoutine(IEnumerator coroutine)
          {
              while (true)
              {
                  try
                  {
                      if (!coroutine.MoveNext())
                      {
                          yield break;
                      }
                  }
                  catch (Exception e)
                  {
                      this.e = e;
                      yield break;
                  }
                  object yielded = coroutine.Current;
                  if (yielded != null &amp;&amp; yielded.GetType() == typeof(T))
                  {
                      returnVal = (T)yielded;
                      yield break;
                  }
                  else
                  {
                      yield return coroutine.Current;
                  }
              }
          }
      }
  
      IEnumerator Start()
      {
          var routine = StartCoroutine&lt;int&gt;(TestNewRoutineGivesException());
          yield return routine.coroutine;
          try
          {
              Debug.Log(routine.Value);
          }
          catch (Exception e)
          {
              Debug.Log(e.Message);
              Debug.Break();
          }
      }
  
      IEnumerator TestNewRoutineGivesException()
      {
          yield return null;
          yield return new WaitForSeconds(5f);
          UnityWebRequest www = UnityWebRequest.Get("http://localhost:3000/api/players");
          www.downloadHandler = new DownloadHandlerBuffer();
          yield return www.Send();
          while (!www.downloadHandler.isDone) yield return new WaitForEndOfFrame();
  
          if (www.isError)
          {
              Debug.Log(www.error);
          }
          else
          {
              string results = www.downloadHandler.text;
              yield return results;
          }
      }
  }
  </code></pre>
  
  <p>but if for example my TestNewRoutineGivesException looks like this, then it works</p>
  
  <pre><code>IEnumerator TestNewRoutineGivesException()
  {
      yield return null;
      yield return new WaitForSeconds(5f);
      yield return new 100;
  }
  </code></pre>
  
  <p>it will return "100"</p>