Question by
Rotavele · Jun 06, 2016 at 07:49 AM ·
c#scripting probleminstantiatebuttonienumerator
Script loads wrong information
Hi Guys,
This script loads images, and text (their name) into buttons as well as adds a listener to the button. The listener tells the script which image they selected, as well as loads the image to the "Preview Image" on the registration screen. However, It loads the wrong image and sends the wrong info to the script. What's going on here?
IEnumerator GetTheFlags() {
WWWForm FlagForm = new WWWForm();
FlagForm.AddField("type", FlagsType);
WWW GetFlagsData = new WWW("http://site.com/GetCustomFlags.php", FlagForm);
yield return GetFlagsData;
string GetFlagsDataString = GetFlagsData.text;
RetrievedFlags = GetFlagsDataString.Split(';');
GetFlags();
for (int i = 0; i < RetrievedFlags.Length; i++)
{
GameObject button = (GameObject)Instantiate(buttonPrefab);
string Findex = GetFlagData(RetrievedFlags[i], "Nation:");
string imgname = GetFlagData(RetrievedFlags[i], "URL:");
string filePath = Application.persistentDataPath;
filePath += "/" + imgname;
string loadFilepath = filePath;
if (System.IO.File.Exists(filePath))
{
string pathforwww = "file://" + loadFilepath;
//Debug.Log("TRYING FROM CACHE file " + pathforwww);
WWW FlagImg = new WWW(pathforwww);
yield return FlagImg;
button.GetComponentInChildren<Text>().text = Findex;
button.GetComponent<Button>().onClick.AddListener(
() => { SetFlag(Findex, imgname); }
);
button.GetComponent<RawImage>().texture = FlagImg.texture;
button.GetComponent<RawImage>().SetNativeSize();
//button.transform.parent = menuPanel;
button.transform.SetParent(menuPanel);
}
else
{
WWW FlagImg = new WWW(flagsURL + imgname);
yield return FlagImg;
button.GetComponentInChildren<Text>().text = Findex;
button.GetComponent<Button>().onClick.AddListener(
() => { SetFlag(Findex, imgname); }
);
System.IO.File.WriteAllBytes(filePath, FlagImg.bytes);
button.GetComponent<RawImage>().texture = FlagImg.texture;
button.GetComponent<RawImage>().SetNativeSize();
//button.transform.parent = menuPanel;
button.transform.SetParent(menuPanel);
}
}
}
}
The SetFlag (What the user is supposed to call when they click the button prefab)
void SetFlag(string Findex, string flagurl) { RegistrationController.Flag = Findex; Debug.Log("Flag Selected:" + Findex); Debug.Log("Flag Imposed:" + RegistrationController.Flag); StartCoroutine(SetPreviewFlag(flagurl));
}
Comment