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:

Hi, didn't tested it but this information should be in data.button : http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.PointerEventData.html

You can use "name" to get the name of a game object. http://docs.unity3d.com/ScriptReference/Object-name.html void SendMyName(){ otherscript.ReceiveName(gameObject.name); } public void ReceiveName(string buttonName){ Debug.Log(buttonName); }

I know how to get a name of a predefined object, but the problem I am having is getting the name from my instanced button. If I have 17 different buttons, each with its own name, I want 1 script that somehow can read the name of whichever button was clicked.

7 Answers

7

Namespace: UnityEngine.EventSystems

It gives the name of the button clicked.

EventSystem.current.currentSelectedGameObject.name

Cheers :slight_smile:

Works like a charm; 2 lines of code and no major changes in my code; thanks!

Perfect - thank you!

Awesome, thanks!

You are the real MVP.

1000 thanks!

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);
    }

I was sure there has to be some simple answer. Thank you very much! Also, I didn't know you can pass parameters through Event Trigger. Wish I knew before finishing my app :|

Hi ! Its not working for me Can we use Button argument in OnClick() function

Thanks!! It worked perfectly : )

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

While I appreciate your responses, unfortunately they are not quite what I'm looking for. Both of those examples you gave me assume you can somehow link those items to themselves in the editor. My buttons are getting instantiated and deleted from the scene frequently, and so I cannot link them to themselves in order to get their name. Its possible that what I'm trying to do is not possible, but it seems silly that it wouldnt be.. How do I get the name of the object I clicked on without having to link the actual gameobject to themselves?

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.

That is perfect, thank you so much!

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?