- Home /
4.6 adding onClickListener to instatiated buttons on a scrollview and click first button then it contain last index value of delegate method
hi.. this is my code and there is a problem . Buttons are added to the scroll-view and when I clicked first button and I print value of in SelectSeachProfile Method then printed value of last button index that is when we clicked any buttons then printed value is last index value. For example in my case 3 buttons are created and click on 1st button then printed value is 2 in SelectSeachProfile Method and click any other buttons then value remains same that is 2. So anyone have idea to solve it ? i f have please mail me or post your answr - my emailid - anwarhusainbly@gmail.com thanks in advance..
for(int i = 0; i < frndreqJson["results"].Count ; i++)
{
//----------Loading friend pic from server------------------------
Texture2D texture = new Texture2D(1024, 1024, TextureFormat.RGB24, false);
www = new WWW(frndreqJson["results"][i]["picData"]);
yield return www;
www.LoadImageIntoTexture (texture);
image = Sprite.Create (texture, new Rect(0, 0, texture.width, texture.height), new Vector2 (0.5f, 0.5f));
currentResult = new Profile();
currentResult.id = int.Parse (frndreqJson["results"][i]["id"]);
currentResult.name = frndreqJson["results"][i]["name"];
currentResult.surname = frndreqJson["results"][i]["surname"];
Debug.Log ("id = "+currentResult.id+" Name = "+currentResult.name+" Surname = "+currentResult.surname);
profileResults.Add (currentResult);
buttons[i] = (Instantiate (ButtonObject) as GameObject).GetComponent<UnityEngine.UI.Button>();
buttons[i].transform.SetParent(ScrollView.transform);
buttons[i].transform.localScale = Vector3.one;
buttons[i].GetComponentsInChildren<Image>()[1].sprite = image;//ShakeController.Texture2DToSprite(frndpic);
buttons[i].GetComponentInChildren<Text>().text = currentResult.name+" "+currentResult.surname;
int j = i;
UnityEngine.UI.Button.ButtonClickedEvent frndonClick = buttons[i].GetComponent<UnityEngine.UI.Button>().onClick;
frndonClick.AddListener(delegate { SelectSearchProfile(j); });
//frndonClick.AddListener ( () => SelectSearchProfile (j));
//buttons[i].GetComponent<Button>().onClick.AddListener (() => {SelectSearchProfile (temp);});
}
Comment