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");
}
}
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:
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”.