I'm working on Unity and trying hard to make a custom SelectableButton that stores method(s) in two different delegates, and executes one whenever the button is selected and the other when it is deselected. The relevant part shows like that :
public delegate void OnSelectAction();
public delegate void OnDeselectAction();
public class SelectableButton : MonoBehaviour
{
private bool selected;
public OnSelectAction onSelect;
public OnDeselectAction onDeselect;
public bool Selected
{
get { return selected; }
set
{
selected = value;
if(selected)
onSelect();
else
onDeselect();
}
}
}
On another script, I try to encapsulate a method in onSelect and another in onDeselect.
for(int i = 0; i < racesData.RaceList.Count; i++)
{
selectableButton.onSelect = Select(racesData.RaceList[i]);
selectableButton.onDeselect = Deselect(racesData.RaceList[i]);
}
public void Select(Race race)
{
// Does its selection stuff
}
public void Deselect(Race race)
{
// Does its deselection stuff
}
In the end, I end up with an error telling me that I can't "convert void to OnSelectAction/OnDeselectAction". And if I try to write it rather this way :
selectableButton.onSelect = () => Select(racesData.RaceList[i]);
selectableButton.onDeselect = () => Deselect(racesData.RaceList[i]);
The Select() and Deselect() methods don't execute the way they are supposed to. If someone could help in giving me the part that I'm lacking to make this work, it would be nice and I'd be grateful for the hand :)