Simple http Request

Hello,
I’m new to Unity and C#. I’m familiar with programming, but C# is foreign to me. I plan on going through the proper steps to learn it, but was wondering if I could ask for help with a basic http request in the meantime. I have searched this out and spent way too many hours on this with not much to show.

I have succeeded in getting an event handler to respond to button presses on the UI I’ve created. My setup so far is as follows:

Hierarchy:
-Main Camera
-Canvas
-----Button
---------Text
-----_Manager
-----EventSystem

_Manager is the object that handles the buttonClick script.

I have a script called buttonClick.cs that contains the following code:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class buttonClick : MonoBehaviour {
   
    public void react () {
        Application.LoadLevel (Application.loadedLevel);
        Debug.Log ("Pressed!");
    }
}

All of this works well. The next, and really the last thing I need to do, is make a simple http GET request to my server, we’ll call it mysite.com. I have my server setup to return a short, one word string. I want to store this in a variable for reference, lets say myVar.

Can what I want to do be build into my react () function? If it needs to be restructured, can someone show me a solution in this scenario?

Thank your very much for taking the time to look at this.

2293009--154255--Hierarchy.PNG
2293009--154256--Assets.PNG

The main thing is that if you fire off the WWW request, by the time it comes back, the scene will have changed and your original script object will be long gone.

What you want is to make a non-Monobehavior script (with mostly static functions) that is a central web services handling type object. It would have a coroutine to yield on the WWW object waiting for it to complete or error out, and writing it down into either a message queue somewhere, or else a static variable saying “here’s the last good thing through.”

Generally you want to plan ahead for lots of separate WWW threads going at once, which precludes a single static response variable, but it might get you up and running to try the static route for simplicity first, until you get a bit of functioning time with it and develop a feel for how to make it concurrent and multi-request compliant. There are plenty of examples of web request dispatching code around, so I will let you search that up and get a feel for what you’re up against here.