Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
12
Question by EsoEs · Nov 11, 2014 at 02:47 AM · button4.6

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! :)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _dns_ · Nov 11, 2014 at 03:24 AM 0
Share

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

avatar image b1gry4n · Nov 11, 2014 at 03:25 AM 0
Share

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

avatar image EsoEs · Nov 11, 2014 at 03:40 AM 0
Share

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 Replies

· Add your reply
  • Sort: 
avatar image
90

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 :)

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darshie1976 · Feb 28, 2016 at 08:43 AM 1
Share

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

avatar image SoftwareLogicAU · Mar 17, 2016 at 07:41 AM 0
Share

Perfect - thank you!

avatar image Vystyk · May 16, 2016 at 07:45 PM 0
Share

Awesome, thanks!

avatar image j_henry · Mar 09, 2017 at 02:53 PM 0
Share

You are the real $$anonymous$$VP.

avatar image BjoUnity3d · Sep 07, 2020 at 03:07 PM 1
Share

This works but only if you are using navigation on the button (if it's set to None currentSelectedGameObject is null).

Show more comments
avatar image
16

Answer by sathish261279 · Feb 12, 2015 at 12:17 PM

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);
     }
    
    
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mihai-dbpx · Aug 20, 2015 at 01:54 PM 0
Share

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 :|

avatar image Anonymous789 · Apr 30, 2016 at 10:54 AM 0
Share

Hi ! Its not working for me

Can we use Button argument in OnClick() function

avatar image JG0328 · Dec 31, 2016 at 03:38 AM 0
Share

Thanks!! It worked perfectly : )

avatar image
5

Answer by vfxjex · Dec 26, 2015 at 07:17 AM

 string name =  EventSystem.current.currentSelectedGameObject.name;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EsoEs · Nov 11, 2014 at 02:41 PM 0
Share

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?

avatar image _dns_ · Nov 11, 2014 at 03:31 PM 1
Share

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)

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't change UI Button Listeners 1 Answer

[4.6 b20] How to make a hold button for the new UI 4 Answers

ArgumentException: method return type is incompatible 1 Answer

How to create a button for every supported resolution? 1 Answer

4.6 UI Change what 'Spacebar' is doing by default 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges