Unfortunately, it looks like this isn’t being updated at all in the game object - clicking into it via the unity interface doesn’t show any update on the button function, nor does the button itself call the method I’ve passed through via the AddListener function.
Here is what I did. I had a prefab button I want to Instantiate and then addListener, but what I did was add the listener to the prefab and not to the clone I just Instantiated.
public GameObject prefab; //prefab you want to Instantiate
public GameObject obj; //the Instantiated clone
void Start(){
obj = Instantiate(prefab, parent)
obj.GetComponent<Button>().onClick.AddListener((delegate { yourMethod();)); //or the lambda way (() => {yourMethod();})
//mistake I made
prefab.GetComponent<Button>().onClick.AddListener(delegate stuff) //this is what caused my problem.
}
I got to fancy with naming variables and should have used simpler names like obj and prefab.
I have one solution for someone who encounters the same sort of the problem.
If you want to create Prefab of game object, which will contain button or buttons as components of parent itself, or inside of his childrens, and you want to initiate it’s Listeners with onClick = () => { someDelegate}; And then instantiate them and catch their Listeners all finely working.
Just add this method:
GameObject InstantiateWithListeners
By the way, to get Button component in child or set a bunch of buttons, I will recommend you to create simple MonoBehaviour component on parent game object and add to him public List of Buttons. After that, Initiate this prefab with all needed button components in inspector. Now you can easily iterate on every matching Button component inside InstantiateWithListeners method and initiate it with proper Listener as was shown above.
First, don’t use Transform.FinsChild, it is deprecdated, you can look straight for a button component in a child.
Second, use simple lambda function if you want to pass a parameter to other methos.
The previously listed solutions didn’t seem to work for me, so here is what I came up with.
My problem anyway seemed that it tried to add the listener after the button was pressed and by that time, the variables used in the button would have changed.
So I made a new script that was on the Button Prefab, so I can add the function in the prefab.
And when I created the Button prefab, I only need to change the variables in the script instead of changing the button.
script on the Prefab where the Button is on:
public int i;
public MonoBehaviour mon;
public void ButtonPressed()
{
mon.function(i);
}