Instantiated button prefab causes Delegation error on click and does not add OnClick Listener
Currently trying to make a GUI that generates a button ( MagicAttackButton ) based off of a prefab ( magicSelectButton ) and adds said button to a list ( atkBtns ). When the button is instantiated, the script that spawns them also assigns a method to it to make a simple panel appear ( SwitchToMagicAttacks() ).
The issue I'm having is that the onClick.AddListener doesn't seem to do anything. When the button instantiates, the OnClick() section is blank.
When I click on the button I get an error that states :
[ArgumentException: method arguments are incompatible] along with a loooong list of System.Delegate and System.Boolean related errors. Nothing else happens after that and it occurs every time I click the button.
Here is the current code for spawning a button:
GameObject MagicAttackButton = Instantiate(magicSelectButton) as GameObject;
MagicAttackButton.GetComponent<Button>().onClick.AddListener(() => SwitchToMagicAttacks());
MagicAttackButton.transform.SetParent(ActionSpacer, false);
atkBtns.Add(magicSelectButton);
And here is the simple method :
public void SwitchToMagicAttacks()
{
ActionPanel.SetActive(false);
MagicPanel.SetActive(true);
}
For context, the GUI is all spawned through the same script which is allocated what prefabs to spawn and what HUD elements to use. Both the button spawning and SwitchToMagicAttacks() methods are inside this script. The button prefab does not spawn with any scripts of its own.
If anyone has any possible solutions I'd really appreciate it!