- Home /
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:
EventSystem.current.currentSelectedGameObject = null on debug this function.
Result:
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!
Well, your code should work. You're using NGUI, right? Could you post a screenshot of the button inspector setup?
Hi, please see the question updated! Thanks in advance!
Where do actually you call OnClickFunction
in your code? I can't see it anywhere.
Is there any particular reason you can't pass an argument to Click()?
The biggest question is redundancy of always having to pass the button name , knowing that this event already linked to it.
Answer by IgorUnity3D · Jul 16, 2016 at 06:55 AM
And I found the problem in my case:
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!
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..
I just spent hours searching for this answer, and was about to give up. Thank you so much!
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);
}
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
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:
And this is the 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".
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!
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.