Get value from array in unity: debug works, value's changing, but color not changing
I want to call some values in array from mysql through a UI-Button. If the array contains strings equal to button's name, the button will pick values from the array. This value ( 0 & 1) will be translated into slider's value which is child of the button and it will change its color accordingly. if the slider value = 1, button color = green, if 0 = red.
The problem is i couldn't change the button color. Changing the value works, debug works too. This should be very simple, but i don't know why it doesn't work. This is the script attached in the button:
public string[] data;
void Start () {
StartCoroutine (loadStart ());
}
public IEnumerator loadStart (){
yield return new WaitForEndOfFrame ();
WWW sliderData = new WWW ("localhost/unitypb/slider.php");
//wait until download finish
yield return sliderData;
//get data as text
string sliderString = sliderData.text;
//print (verantString);
data = sliderString.Split (';');
foreach (string x in data) {
if (x.Contains (this.gameObject.name + "-KW" + KWtext.text)) {
int index = Array.IndexOf (data, x);
int savedValue = Convert.ToInt32 (GetDataValue (data [index], "Value:"));
this.GetComponentInChildren <Slider> ().value = savedValue;
if (this.GetComponentInChildren <Slider> ().value == 1) {
this.GetComponent <Image> ().color = Color.green;
}
if (this.GetComponentInChildren <Slider> ().value == 0) {
this.GetComponent <Image> ().color = Color.red;
}
Debug.Log (Convert.ToString (this.gameObject.name + "-KW" + KWtext.text));
} else if (!x.Contains (this.gameObject.name + "-KW" + KWtext.text)) {
this.GetComponent <Image> ().color = Color.white;
Debug.Log ("not available");
}
}
}
public string GetDataValue (string data, string index) {
string value = data.Substring (data.IndexOf(index)+index.Length);
if(value.Contains("|"))
value = value.Remove (value.IndexOf ("|"));
return value;
}
However, if I remove this line the script would run just fine. But I would need it for other similar functions so i can't remove it.
else if (!x.Contains (this.gameObject.name + "-KW" + KWtext.text)) {
this.GetComponent <Image> ().color = Color.white;
Debug.Log ("not available");
}
Your answer
Follow this Question
Related Questions
SOLVED : How to scan and delete elements from a name/score array on a dreamlo database 2 Answers
Delete data in mysql 0 Answers
Check if a object with a specific tag exists in an array 1 Answer
How to Synchronise between Server Database (MYSQL) and Local database (SQLite) 1 Answer
Unity sql PHP script can log in with wrong password, what's wrong? 0 Answers