Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by IgorUnity3D · Jul 14, 2016 at 04:36 PM · uibuttoneventsonclick

UI Button OnClick Function - How to get name of button that was clicked?

How can I get the button name that is being clicked, without having to pass a parameter to the function?

[UPDATED]

I'm using Unity 5.4.0b24 (64-bit)

 UnityEngine.EventSystems;
 public class EventManager
 {
   public void OnButtonClick()
   {
       var go = EventSystem.current.currentSelectedGameObject;
        if (go != null)
           Debug.Log("Clicked on : " + go.name);
         else
           Debug.Log("currentSelectedGameObject is null");
    }
 }

See screenshot of the button inspector setup:

alt text

EventSystem.current.currentSelectedGameObject = null on debug this function.

Result:

alt text

 currentSelectedGameObject is null
 UnityEngine.Debug:Log(Object)
 EventManager:OnButtonClick() (at Assets/Scripts/EventManager.cs:19)
 UnityEngine.EventSystems.EventSystem:Update()

Is there any property that can be accessed? Since gameObject.name returns the name of GameObject on Script class as attached (Test.cs).

How can I get the information without using Event Trigger? Its possible?

Thanks in advance!

stru-1.png (132.2 kB)
stru-2.png (95.3 kB)
Comment
Add comment · Show 9
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 wojtask12 · Jul 14, 2016 at 08:14 PM 0
Share

Well, your code should work. You're using NGUI, right? Could you post a screenshot of the button inspector setup?

avatar image tomekkie2 wojtask12 · Jul 14, 2016 at 09:12 PM 0
Share

Agree, I tested and it does work for me.

avatar image IgorUnity3D wojtask12 · Jul 15, 2016 at 02:13 AM 0
Share

Hi, please see the question updated! Thanks in advance!

avatar image wojtask12 IgorUnity3D · Jul 15, 2016 at 09:16 AM 0
Share

Where do actually you call OnClickFunction in your code? I can't see it anywhere.

Show more comments
avatar image Arkaid · Jul 15, 2016 at 02:34 AM 0
Share

Is there any particular reason you can't pass an argument to Click()?

avatar image IgorUnity3D Arkaid · Jul 15, 2016 at 11:51 AM 0
Share

The biggest question is redundancy of always having to pass the button name , knowing that this event already linked to it.

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by IgorUnity3D · Jul 16, 2016 at 06:55 AM

And I found the problem in my case:

alt text

I only changed the Navigation option of Button component 'None' to 'Automatic' (or any other), and all went to work!

Thanks for all the answers!


solved-stru.png (11.0 kB)
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 better_walk_away · Oct 18, 2018 at 03:52 AM 0
Share

Yes! This is the solution to me. I am speechless now, how do we know that the problem was caused by the Navigation setting... We only can trial & error ourselves..

avatar image nglasl · Oct 19, 2021 at 10:22 AM 0
Share

I just spent hours searching for this answer, and was about to give up. Thank you so much!

avatar image
1

Answer by Pillo · Jul 14, 2016 at 10:05 PM

Yo, maybe you will have to test nullity of variables before using them (:

     void Update ()
     {
         var currentEventSystem = EventSystem.current;
         if(currentEventSystem == null) { return; }
 
         var currentSelectedGameObject = currentEventSystem.currentSelectedGameObject;
         if(currentSelectedGameObject == null) { return; }
 
         Debug.Log(currentSelectedGameObject.name);
     }
Comment
Add comment · Show 1 · 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 IgorUnity3D · Jul 15, 2016 at 02:15 AM 0
Share

Hi Pillo, The verification of the object is not the problem here! The question is that it will always be null on function. EventSystem.current.currentSelectedGameObject = NULL EVER

avatar image
1

Answer by Bunny83 · Jul 15, 2016 at 09:55 AM

I just created this simple script:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class UI_Event_Receiver : MonoBehaviour
 { 
     public void OnButtonClick()
     {
         var go = EventSystem.current.currentSelectedGameObject;
         if (go != null)
             Debug.Log("Clicked on : "+ go.name);
         else
             Debug.Log("currentSelectedGameObject is null");
     }
 }

I attached it to the MainCamera (any other object would work as well). I created two buttons and set the OnClick event of both buttons to that method:

Assignment

And this is the result:

Result

The way you present your question is confusing as others have already said. You have two different classes and the one class that you actually use (your EventManager) doesn't even use "currentSelectedGameObject". In my example i never get the message "currentSelectedGameObject is null".


onclickevent.png (48.0 kB)
onclickevent2.png (26.1 kB)
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 IgorUnity3D · Jul 15, 2016 at 11:47 AM 0
Share

The code on question is "sample" Test.cs not is my real class... "Event$$anonymous$$anager.cs". I updated the question, please see! Your images is broken... you can fix it? Thank you so much!

avatar image Bunny83 IgorUnity3D · Jul 15, 2016 at 01:04 PM 0
Share

What do you mean by "broken"? I host my images on my public dropbox and i can view the site even over several webproxy servers without any problems. Anyways i just uploaded the images directly here on UA. You may need to refresh the page / clear the cache (usually CTRL+R)

I still don't get where the problem is. You didn't show us your actual hierarchy and you haven't mentioned where that script is attached to.

avatar image IgorUnity3D Bunny83 · Jul 15, 2016 at 10:05 PM 0
Share

Please, see the updated question! Thanks

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

10 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

Related Questions

Unity 5.0.2f1 UI Button OnClick Function 6 Answers

UI Button generated in runtime OnClick event 0 Answers

UI: Mouse events not working 1 Answer

Can't change UI Button Listeners 1 Answer

How do change UI.button onclick states(off, editor and runtime, runtime only) via script? 0 Answers


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