- Home /
 
WWW class does not work
First of all. Im new to Unity and im developing in the new Unity 5.0.0. I´ve been looking at the WWW class in Unity documentation and followed through it and havent got it to work yet. I have looked in other questions and googled alot and couldnt get it to work. This is the code I got in my APImanager:
using UnityEngine; using System.Collections;
public class apiManager : MonoBehaviour
{
public string url = "URL";
 public string temp;
 public void Start(){
     WWW w = new WWW (url);
     StartCoroutine (WaitForRequest (w));
                  
 }
 IEnumerator WaitForRequest(WWW w){
     yield return w;
     temp = w.text.ToString ();
 }
 public string getTemp(){
     return temp;
 }
 
               }
And in my main file where I want to call the string getTemp method and show the data in a label.. it doesnt work. Nothing shows up and im struggling to figure it out (Im trying to show the data in the label as a text (string).
public class main : MonoBehaviour
{
apiManager myApiManager = new apiManager(); }
void onGUI
{
GUI.Label(ScreenPosition(0, 500, 300,300), myApiManager.getTemp());
}
Is it something im missing or what? Can you please provide me information on how it should work or if I have missed something? Thanks.
Answer by goodbadwolf · Apr 15, 2015 at 12:12 PM
myApiManager is a MonoBehaviour and should not be instantiated using "new". Attach it to a GameObject in your scene and connect it to the reference in main.
 public class main : MonoBehaviour
 {
   public apiManager myApiManager;
 
   void onGUI
   {
       GUI.Label(ScreenPosition(0, 500, 300,300), myApiManager.getTemp());
   }
 }
 
              Thank you. That and some code changes, as putting the WWW object inside the WaitForRequest(), and removing the "ToString();" after w.text.. That did the work. Thanks alot!
Your answer
 
             Follow this Question
Related Questions
WWW class and HTTP Headers 3 Answers
Unable to call external API (IBM Watson) via HTTP request? 1 Answer
Calling API via proxy 0 Answers
Is pulling and parsing XML data from 3rd party API call possible? 1 Answer
UriFormatException: Invalid URI: Invalid port number for VirtualBox server using IPv6 Address 1 Answer