Hi, I know that similar questions have been asked, but my question is with a yield www
function.
I can load an image from a URL, into a texture, like this:
public void LoadImage(String img_URL, Texture2D tex=null)
{
www = new WWW(img_URL);
StartCoroutine(WaitForImageLoad(www, tex));
}
IEnumerator WaitForImageLoad(WWW www, Texture2D tex=null)
{
yield return www;
www.LoadImageIntoTexture(tex);
}
But when I try to do the same thing with a String, only the local copy is modified, but not the real variable referenced by the argument theString
, in the below code :
public void LoadString(String URL, String theString=null)
{
www = new WWW(URL);
StartCoroutine(WaitForStringLoad(www, theString));
}
IEnumerator WaitForStringLoad(WWW www, String theString=null)
{
yield return www;
theString = www.text;
}
What I’m trying to do :
These functions belong to a script called AjaxScript.cs
, and I’m trying to initialize the variables of PlayerScript.cs
, like this:
using UnityEngine;
using System;
using System.Collections;
public class PlayerScript : MonoBehaviour {
//public variables:
public AjaxScript AJAX;
public Texture2D PlayerPhoto;
public String PlayerName="not_defined";
// Use this for initialization
void Start () {
AJAX = gameObject.GetComponent<AjaxScript>();
AJAX.LoadImage("http://image.com/image.jpg",PlayerPhoto);
// this works, i.e. PlayerPhoto becomes image.jpg
AJAX.LoadString("http://localhost/my_unity_php_agent.php",this.PlayerName);
//but this does not work, i.e. PlayerName remains "not_defined"
}
}
How can I pass PlayerName as a reference and change it direclty in AjaxScript ?
Thanks !