-1

I have just jumped to Unity and I am doing some experiments to retrive data from an API

Below you can see the test code could you please help me to understand if i am the right way or not please?, because probably i am missing the mapper isn't it ?

Thanks in advance.

  void Start()
    {    
        comments= StartCoroutine(this.GetComments("https://jsonplaceholder.typicode.com/comments"));
        Debug.Log(comments);
    }

   private IEnumerator Comments GetComments(string url) {

        List<Comments> returnComments = new List<Comments>();

        UnityWebRequest comments = UnityWebRequest.Get(url)
        comments.SendWebRequest();

        while (comments.MoveNext())
        {
            var comment = comments.Current;                    
            returnItems.Add(comment);
        }

        return returnComments;
    }
KYL3R
  • 3,877
  • 1
  • 12
  • 26
Gelso77
  • 1,763
  • 6
  • 30
  • 47

1 Answers1

0

Yield the SendWebRequest function.

yield return webRequest.SendWebRequest();

Access the data through the download handler.

var dataString = webRequest.downloadHandler.text;

Full sample code in the docs UnityWebRequest.Get

hijinxbassist
  • 3,667
  • 1
  • 18
  • 23