Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by sp1N · Nov 12, 2016 at 07:25 PM · buttononclick

How to handle multiple button on addListener

Hi, i already search this question on the forum and the solution given doesn't work for me. When i call the addListener function for all my buttons and when i click on any button i always have the same one coming back : "Button6".

I did try this : bout = b.GetComponent (); because i call a delegate function this means i need to serialize the variable to be updated but it doesn't work.

Here my function to add listener on everybutton :

 public static void addListenerButton(GameObject canvas){
 
         Component[] lesBoutons;
         Button bout;
         lesBoutons = canvas.GetComponentsInChildren<Button> ();
 
         foreach (Component b in lesBoutons) {
             bout = b.GetComponent<Button> ();
             b.GetComponent<Button>().onClick.
             AddListener (delegate{taskOnClick(canvas, "y", bout);});
             b.tag = "ButtonPage1";
         }
             
     }

And here my function taskOnClick :

     public static void taskOnClick(GameObject canvas, string jsonFlux, Button leButton){
         Debug.Log (leButton);
 
         Debug.Log ("You have clicked the button!");
         GameObject rawImageGO = new GameObject ();
         rawImageGO.transform.parent = canvas.transform;
         rawImageGO.AddComponent<RawImage> ();
         RawImage rawImageRT = rawImageGO.GetComponent<RawImage> ();
         rawImageRT.rectTransform.sizeDelta = new Vector2 (1500F, 1000F);
         rawImageRT.rectTransform.position = canvas.GetComponent<RectTransform> ().position;
         rawImageGO.name = "StreamingVideo";
 
 
 
     }

Thank's in advance.

I'm using c# btw.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by sp1N · Nov 13, 2016 at 10:24 AM

I'm back because i solve my problem. If someone got the same problem he will be happy to know how to solve it. Here's the code :

     public static void addListenerButton(GameObject canvas){
 
         Button[] lesBoutons;
         string bout;
         lesBoutons = canvas.GetComponentsInChildren<Button> ();
 
         foreach (Button b in lesBoutons) {
             addListener(canvas, b);
             b.tag = "ButtonPage1";
         }
             
     }
 
     public static void addListener(GameObject canvas, Button leButton){
         leButton.onClick.AddListener (() => taskOnClick (canvas, "y", leButton.name));
     }
 
     public static void taskOnClick(GameObject canvas, string jsonFlux, String leButton){
         Debug.Log (leButton);
 
 
         Debug.Log ("You have clicked the button!");
         GameObject rawImageGO = new GameObject ();
         rawImageGO.transform.parent = canvas.transform;
         rawImageGO.AddComponent<RawImage> ();
         RawImage rawImageRT = rawImageGO.GetComponent<RawImage> ();
         rawImageRT.rectTransform.sizeDelta = new Vector2 (1500F, 1000F);
         rawImageRT.rectTransform.position = canvas.GetComponent<RectTransform> ().position;
         rawImageGO.name = "StreamingVideo";
 
 
 
     }

Comment
Add comment · Share
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
avatar image
0

Answer by Glurth · Nov 12, 2016 at 07:37 PM

Not sure if this is the issue, but this seems a bit odd:

 bout = b.GetComponent<Button> ();

because b IS already the button component. In fact, I would recommend you declare lesBoutons as an array of Buttons, rather than Components. Then, inside the loop, you can simply use b, for you button reference:

 foreach (Button b in lesBoutons) {
     b.onClick.AddListener(... b ....);}
Comment
Add comment · Show 1 · Share
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
avatar image sp1N · Nov 12, 2016 at 08:33 PM 0
Share

Thanks for your reply. But i already tried this and it doesn't work. When i click on the "button1" my Debug.Log on the taskOnClick function said it's "Button6" for all buttons i clicked.

avatar image
0

Answer by bongert · Oct 08, 2018 at 12:38 PM

Hey, I think I'm facing the same problem right now. But I don't really get your solution, you just create the method addListener()? Doesn't work for me... At the moment I'm instantiating the buttons in one script:

 public void SetPhrase(string[] phrases)
     {
         Button[] textBut = new Button[phrases.Length];
         for(int i=0; i<phrases.Length; i++)
         {
             int closureIndex = i;
             textBut[closureIndex] = Instantiate(textButton, contentPanel, false);
             textBut[closureIndex].transform.Find("Text").gameObject.GetComponent<Text>().text = phrases[closureIndex];
         }
     }

So far so good, but when TaskWithParameters() is called in the script attached to the Button-Prefab the output is always the one of the last instantiated button:

 public class ClickScript : MonoBehaviour
 {
     public Button button;
     public string phrase;
 
     void Start()
     {
         button = gameObject.GetComponent<Button>();
         phrase = gameObject.transform.Find("Text").gameObject.GetComponent<Text>().text;
         addListener(button, phrase);
     }
 
     public static void addListener(Button b, string p)
     {
         b.onClick.AddListener(() => TaskWithParameters(b, p));
     }
 
     public static void TaskWithParameters(Button b, string p)
     {
         print(b.GetComponent<ClickScript>().buttonIndex + ". Button clicked");
         GameObject.FindWithTag("Player").GetComponent<Talking>().Sprich(p);
     }
 }

But the public string of each button ist the right one! Just TaskWithParameters doesn't work properly. If you know what I'm doing wrong let me know, thanks :-)

Comment
Add comment · Share
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

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

86 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

hi guys who can help me?(i am a persian boy so ... Readable plzz) 1 Answer

Malfunctioned Button 0 Answers

How can I generate and setup buttons via code 1 Answer

Get value in button OnClick() 1 Answer

changing the selected game object in the on click field via script? 0 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