I built a slightly modified version of the Server Side HighScore code (from the wiki) which fetches the user’s high score from the database based on his playerpref username.
So my IEnumerator looks like this:
IEnumerator GetUserScore(string username)
{
Debug.Log ("Loading scores");
string get_url = userScoreURL + "user=" + WWW.EscapeURL(username);
Debug.Log (get_url);
WWW hs_get = new WWW(get_url);
yield return hs_get;
if(hs_get.error != null)
{
print("There was an error getting your score: " + hs_get.error);
}
else
{
Debug.Log(username + " : " + hs_get.text); // used for testing
}
}
Since the IEnumerator isn’t public I decided to make a public method which starts the coroutine and can be called from the outside:
//method to call from outside
public void GetUserScoreCoRoutine(string username)
{
StartCoroutine(GetUserScore(username));
}
I’m accessing the IEnumerator from a different script through the CoRoutine like this:
public class DisplayUserScore : MonoBehaviour {
public HSController HSScript;
void Start () {
username = PlayerPrefs.GetString("Test");
HSScript = GetComponent<HSController>();
HSScript.GetUserScoreCoRoutine(username);
void OnGUI(){
GUI.Label (new Rect(50,50,200,50), "Personnal Global Score: ");
// I WANT THE SCORE FROM DB TO APPEAR IN A GUI LABEL HERE
}
}
I know that fetching the score works because I can output it successfully in console from the original script. (so the PHP and DB are ok). But how can I access hs_get.text from my DisplayUserScore script? I’m not too familiar with IEnumerator so what I’m trying to do might not be actually doable.
Thanks for the help!