4.6 How to get name of button that was clicked?

So Im instantiating a number of buttons, they each are getting a unique name. However, when I click that button, I need to pass their unique name to another function as a string. Is there a paramenter I can add to the ‘public void OnPointerDown(PointerEventData data)’ method to capture this information? I’m very new to Unity and C# so please be clear, thanks! :slight_smile:

Namespace: UnityEngine.EventSystems

It gives the name of the button clicked.

EventSystem.current.currentSelectedGameObject.name

Cheers :slight_smile:

Unity C#

  1. Add Event Trigger in Inspector to your Button

  2. click Add New Event Type and add a pointer click event.

  3. drag your gameobject containing this script in the script area.

  4. assign the below function in the function area.

  5. assign the button in the parameter area.

    public void OnClicked(Button button)
    

    {
    print(button.name);
    }

string name = EventSystem.current.currentSelectedGameObject.name;

Hi, ok, my previous comment was wrong: PonterEventdata.button is not a reference to the clicked button.
In my experience, implementing the interfaces IPointerDownHandler or IPointerClickHandler resulted on the OnPointerClick or OnPointerDown(PointerEventData data) functions being called but the parameter “data” is always null. I don’t know if I’m doing something wrong or if it’s a beta bug (as of beta 20). If in your case the OnPointerDown() is called on a component attached to a button, you can still access to the clicked object through this.gameObject.name.

This post may help too: it explains how to register an event on a button click in the editor or by script: [4.6 - UI] Calling function on button click via script - Unity Answers

There are also some examples here: GitHub - AyARL/UnityGUIExamples: Scripting examples for the new Unity GUI

Since you are instantiating these buttons at runtime, an easy way to have reference to all of them is to just add them to a list.

When the button is clicked have that button call a function that searches for itself in the “instantiated buttons list”.

public List<GameObject> buttonList = new List<GameObject>();

void CreateButton(){
	// instantiation code here...
	buttonList.Add(yourbutton);
}
public string WhoAmI(GameObject idontknow){
	string foundButton = "";
	for (int i= 0; i < buttonList.Count; i++) {
		if(idontknow == buttonList*){*

_ foundButton = buttonList*.name;_
_
break;_
_
}_
_
}_
_
return foundButton;_
_
}_
_
//on the button script, trigger the function._
_
void WhatIsMyName(){_
_
string myNameIs = buttonManager.WhoAmI(this.gameObject);_
_
}*_
when using lists you must add:
using System.Collections.Generic;
to the top of your script

On your prefab button add this script


`
using UnityEngine;
using System.Collections;

public class ButtonPushed : MonoBehaviour {

public GameObject SendMessageTo;
public string CallMethod;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}

public void Pushed()
{
	SendMessageTo.SendMessage(CallMethod, gameObject.name);
}

}
`


Set the Buttons OnClick event to ButtonPushed.Pushed

Add the receiving Gameobject and method to call to the above script.

myButton.onClick.AddListener(() => { manager.OneButtonIsClicked( this.gameObject.name ) });

In this line how I can get the index of clicked button in place of name of the button?