how to change a buttons function with a script?
so i got this working already with this line of code
x.GetComponentInChildren<Button>().onClick.AddListener(delegate{ use_item(name); }) ;
my problem is when i click the button in question it will keep calling the function as long has its clicked. basically after holding the mouse down for 3 seconds it will call the function 107ish times (from my debug log).
how do i stop that? how do i set my button to call a function only once?
on a side note I found "delegate" while looking up syntax but I can not find any thing explaining what it dose. if any one can tell me what it does that would be appreciated.
i forgot to mention that I am working with a list of buttons and that line of code would update the name (string variable) in each one.
so about 9 buttons in all each call the same function but send its own string and i have the ability to change that string in game. that is what i'm trying for
Answer by UnityCoach · May 14, 2017 at 08:38 AM
A delegate is a method you subscribe to an event. When you work with C# events, you declare a delegate that represents the signature of the method (the parameters it must be passed), then you subscribe methods that match that signature/delegate. If you don't already implement such method, you can build a delegate and pass it. This is what you do here :
x.GetComponentInChildren<Button>().onClick.AddListener(delegate{ use_item(name); }) ;
delegate {} is a method with no name that you pass. The only problem with this, is that you have no reference to it to later unsubscribe it with UnityEvent.RemoveListener. Your only option then is to use UnityEventBase.RemoveAllListeners to remove all delegates.
Consider doing this :
x.GetComponentInChildren<Button>().onClick.AddListener (OnUseItem);
public void OnUseItem ()
{
use_item(name);
}
Which allows you to later remove it with :
x.GetComponentInChildren<Button>().onClick.RemoveListener (OnUseItem);
Hope this clarifies things a bit.
Although, I'm pretty sure there's another problem as a button shouldn't be called 100 of times if you simply keep it pressed, as onClick event is called when you release the button.
yeah its an odd problem thou i forgot to mention that I am working with a list of buttons and that line of code would update the name (string variable) in each one.
so about 9 buttons in all each call the same function but send its own string and i have the ability to change that string in game. that is what i'm trying for
your suggestion looks like it would change the function all to gather but will try to remove the Listener
so i can't store a reference because i would have no way to dynamically change each button on run time aka all 9 buttons would do the same thing even after i changed the task they would still be synchronized but, x.GetComponentInChildren().onClick.RemoveAllListeners(); was able to fix my problem any way evidently i was storing a new Listener with each loop and changing the string of all the Listeners for each button
Hey, I tried this way and for some reason it just doesn't work, $$anonymous$$y button onClick event doesn't change, It might be because I am working on android, I am pretty stuck there, Any ideas?
Answer by CRISTALSONIC · May 14, 2017 at 04:32 PM
ok that dose not let me dynamically change the name variable and its still calling the function to much so I'm back to were i was
public void Inventory_update() { int inventory_slot = 0; // ok this loop will check to see if the buttons need to be set with the bool values // ant it will get the extra info with the ref variables
foreach (GameObject x in Inventory_list)
{
int stock = 0;
Sprite Item_pic=null;
string name = "";
if (Player_inventory.inventory_set(inventory_slot, ref stock,ref Item_pic,ref name) == true) {
x.SetActive(true);
x.GetComponentInParent<Image>().color = player_data.C_color;
x.GetComponentInChildren<Button>().image.sprite = Item_pic;
//x.GetComponentInChildren<Button>().onClick.RemoveListener(delegate { use_item(name); });
x.GetComponentInChildren<Button>().onClick.AddListener(delegate{ use_item(name);}) ;
x.GetComponentInChildren<Text>().text = stock.ToString();
}
else { x.SetActive(false); }
inventory_slot++;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612121952im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How create detonation system 0 Answers
Change UI button state to "pressed" via script 2 Answers
Referencing a button in script for different game modes in the Space Shooter Tutorial 0 Answers
New to the new UI (buttons) 2 Answers
Basic scripting 1 Answer