Problem with variable from php to c# unity

Hi guys, If I send a variable from php to c# in unity, it works. But if I need this variable in another function , it doesn’t work and I receive “0”.
I tryed to insert the string that c# receive from php page into another variable that I have declared into class, but it not work yet. How could resolve the problem?
this is my code in c#

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    public GUIText highScoreText;
    public string rec;
    public float recf;

    void Awake()
    {      string wwwResponse = "http://localhost/Game/user.php";
        WWW www2 = new WWW(wwwResponse);
        StartCoroutine(send(www2));
          
    }


    void Start()
    {   Debug.Log(recf);
        highScoreText.text = "Local Highscore: " + recf ;
    }

    IEnumerator send(WWW www2)
    {
        yield return www2;
        print(www2.text);
        rec=www2.text;
        recf=float.Parse(rec);
        Debug.Log(recf);
    }

In print(www2.text) and Debug.Log into function IEnumerator send, I receive the information (13) instead in the function Start I receive 0

Could you not set the String above Awake and then run the whole WWW code inside the IEnumerator? Then call the IEnumerator inside Start, it would be more efficient, though you’re running Send on Awake I don’t think Start is receiving it fast enough. You could put all of the code you have inside an IEnumerator, or even put the code inside Start in another function like

 void GetScore()

It should work.

1 Like

That’s because when Start fires, the data hasn’t been received yet.

The whole point of StartCoroutine is to schedule something to be worked on later. The next frame, or a few frames after that, then you’ll get the value.

So, you just need to adjust your expectations (i.e. quit expecting to have a value in Start).

1 Like

Oh thanks, I used the variable into fixedUpdate and it works, thanks for the time