Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ccouper · Mar 29, 2017 at 03:28 PM · textcanvashttpwebrequest

How can I display text from a webrequest?

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'

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Mar 29, 2017 at 05:53 PM 0
Share

your Update method is trying to assign the IEnumerator of GetText() to text and that won't work. Delete the whole Update method and assign the text where you have the Debug.Log reading that out

avatar image ccouper hexagonius · Mar 31, 2017 at 03:36 PM 0
Share

Thanks - it now looks like this:

 else {
             // Show results as text
             Debug.Log(www.downloadHandler.text);
 
             thistext.text = www.downloadHandler.text;
         }

But I get a warning that "Field Rest_test.thistext' is never assigned to, and will always have its default value null'" so my canvas does not get updated. I'm obviously still doing something wrong...

avatar image hexagonius ccouper · Apr 02, 2017 at 11:59 AM 0
Share

the warning is fine. the compiler just doesn't know that the field gets injected.
the question is, do you even reach that part of the code?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aFeesh · Mar 03, 2018 at 05:36 PM

@ccouper

You want to use www.downloadHandler.text and not overwrite the downloadHandler with a new instance. You may also want to use www.SendWebRequest() instead of www.Send()

      IEnumerator GetText() {
          UnityWebRequest www = UnityWebRequest.Get("http://jsonplaceholder.typicode.com/posts/1");
 
          yield return www.SendWebRequest();
  
          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;
          }
      }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image movAX13h · Sep 11, 2019 at 09:41 AM 0
Share

.isError is now .isNetworkError

avatar image
1

Answer by toddisarockstar · Mar 31, 2017 at 11:58 PM

this is how i usually do it:

 using UnityEngine;
 using System.Collections;
 
 public class wwww : MonoBehaviour {
 
     WWW www;
     string txt;
     bool gotit;
     byte[] bytes;
 
     void Start () {
         www = new WWW("http://jsonplaceholder.typicode.com/posts/1");
     }
     
 
     void Update () {
                 if (!gotit){
                       if (www.isDone) {gotit=true;
                 if(www.error==null){
                     bytes=www.bytes;
                     txt = www.text;
                     print (txt);
                     print ("i got "+bytes.Length+" bytes from download");
                 }else{print("I got this error: "+www.error);}
 
             
             }
         }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ccouper · Apr 04, 2017 at 01:26 PM 0
Share

Thanks - this does work but I was hoping to use the newer UnityWebrequest.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

105 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues with OnMouseOver() not working 2 Answers

canvas/text object sync 0 Answers

UI Text alignment mobile vs editor are not equal. 0 Answers

How can I check if there is no free space in the Text component? 1 Answer

Canvas Text Help 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges