Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Anwar_10 · Aug 19, 2015 at 06:55 AM · buttonseventdelegatelisteners

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Check if a listener has already been added to a button? 2 Answers

I want the gameobject field of my persistent listeners to reference an object other than the one they are on. Is this possible? 1 Answer

Navmesh event on navigation end 0 Answers

Question regarding events and event managers. 2 Answers

Play an Animation through a Script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges