[C#] Sending UI Button Name As Parameter On Click

I currently have a game that revolves a lot around the UI, and buttons, while in battle. Currently, my method of doing things is making a method for each button and doing whatever needs to be done from there. For example, my button “Attack” draws the method “AttackPressed()” on click. This worked well until I began to realize that, down the road, all these button are going to require a lot of methods. So, I wondered how I could get the button name I was clicking and save it in a variable to load from a parameter, but I don’t know how I’d go about doing this. I looked up all the methods for the button but they don’t seem to do what I need. Will I have to code this myself, or is there an easier way?

Also, another question that’s not really related to scripting, but what’s the best way to organize a UI? Currently, I’ve been using a system where I disable and enable a canvas. The canvas’s are split between a system of sections, each section has it’s own canvas for me to enable/disable. I feel like there’s an easier way of doing things, but I don’t know of any other methods. Does anyone have any ideas? Thanks.

1 Like

Please help

I’ve been trying multiple solutions to this, combing through Unity Answers, reading about the Unity UI system and watching the videos provided, etc, to no avail.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ButtonTest : MonoBehaviour
{
    public void TestFunction (Button b)
    {
        Debug.Log(b.name);
    }
  
}

Assign this function to the button’s onclick event, and drag the button into the function’s parameter field in the inspector…