- Home /
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! :)
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 Send$$anonymous$$yName(){
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.
Answer by SanthanaBharathy · Nov 18, 2015 at 07:21 AM
Namespace: UnityEngine.EventSystems
It gives the name of the button clicked.
EventSystem.current.currentSelectedGameObject.name
Cheers :)
Works like a charm; 2 lines of code and no major changes in my code; thanks!
This works but only if you are using navigation on the button (if it's set to None currentSelectedGameObject is null).
Answer by sathish261279 · Feb 12, 2015 at 12:17 PM
Unity C#
Add Event Trigger in Inspector to your Button
click Add New Event Type and add a pointer click event.
drag your gameobject containing this script in the script area.
assign the below function in the function area.
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
Answer by vfxjex · Dec 26, 2015 at 07:17 AM
string name = EventSystem.current.currentSelectedGameObject.name;
Answer by _dns_ · Nov 11, 2014 at 02:00 PM
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: http://answers.unity3d.com/questions/777818/46-ui-calling-function-on-button-click-via-script.html
There are also some examples here: https://github.com/AyARL/UnityGUIExamples
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. $$anonymous$$y 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?
There is a way of doing what you want, there is always one. "If there is no solution, it's because there is no problem" ;-)
So, you instantiate some buttons dynamically. There must be a manager that does some Instantiate(some button prefab) or create new gameobject and add components. Why not have a component that you add on each button (either dynamically or in the button prefab) that have a simple script called by the manager just after the instantiation:
public void RegisterListener( my$$anonymous$$anager manager )
{
Button myButton = GetComponent<Button>();
myButton.onClick.AddListener(() => { manager.OneButtonIsClicked( this.gameObject.name ) });
}
OneButtonIsClicked(string) being defined in your manager object.
Then, when one button is clicked, the manager will be called with a parameter that is the name of the clicked gameobject (or anything you want). Don't forget to remove the listener during the OnDestroy of this script with RemoveAllListeners (I guess it will be done anyway but it's always better to cleanup)
Answer by b1gry4n · Nov 11, 2014 at 06:34 PM
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[i]){
foundButton = buttonList[i].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