I have a lot of buttons how can I send the name to identify which one is being pressed?

I’m currently making something similar to a Pokédex meaning I have laods of buttons,

How can I make it so it sends the name of the buttons (when instantiating the buttons it is done in a way like this) PKMN: 1, PKMN: 2 ,etc.

I don’t really want to add the numbers as ints hand by hand on the OnClick() function. but I dont know how to send the name of the button for example or if there is a better approach to this.

Add a parameter of type GameObject and drag the object into the slot. Then inside the listener you have access to the GameObject name:

public void CheckName(string objName)

Button button = buttonContainer.AddComponent<Button>();
string newName= "NewName";
button.name = newName;
		CheckName(button.name); });

You can create your button in a loop, with an update of the naming and each listener will point to the corresponding item.


This line of code will send the name of the gameObject to the console.

Add a Listener to each button like this:

public class ButtonParameter {
     public Button button;
     public int id;
     //and any other data you want
     public ButtonParameter(Button b, int i) {
         button = b;
         id = i;

public void AddButtonListener(Button yourButton, int yourID) {
     ButtonParameter buttonParameter = new ButtonParameter (yourButton, yourID);
     yourButton.onClick.AddListener(delegate { ButtonClickMethod(buttonParameter ); });

public void ButtonClickMethod(ButtonParameter bp) {
     Debug.Log(bp.button.name + ", " + bp.id);