Making modular code for buttons

Let’s say I have three stats: health, stamina, and speed. For each stat, there are buttons to increase or decrease the stat by a specific increment.

For all the ones the increase the stat, I made a different function to increase each stat. My question is this: is there a way for the button to know which stat to increase and have one function for increasing the stats separately, that each button could use the same function?

To continue this example, let’s say late I want to add magic as another stat. I don’t want to have to add another function.


(Note: topics section on this website is acting strange)

One way is to use the following code in your generic increase function:

using UnityEngine;

public class Test3 : MonoBehaviour
    public void ReadButton(string button)

Because you are asking for a string to be passed, when you set up the event in the inspector, do this:


I’ve typed the word “Health” into the parameter field (bottom right) so that the code knows which button called the common function. Do similar for the other buttons and, when you’re ready to add Magic, do the same.