how to get the name of the button?

hello guys,
I have several buttons on the scene. (button1, button2 etc…)
I am working on event trigger component.
I selected the Pointer Enter event and added the script below :

function GetTheButtonName()
{
Debug.Log (this.name);
}

when i run unity,
while i’m moving my mouse on the buttons, console prints as “Canvas”.
How can i get the name of the buttons for each button ?

Thanks for any comments.

You do know you can wire the click event of each button to a method right? But if you do need to get the button names you can do:

    using UnityEngine.UI;
    Button[] ButtonList = this.gameObject.GetComponentsInChildren<Button>();
    foreach (Button aButton in ButtonList)
    {
        Debug.Log(aButton.name);
    }

thanks ramcat,

i am working on this entire day and came to that weird point.
what i wrote here was the first step to where i want to go.

i am using java script and i could not find a source good enough to understand “OnPointerEnter()”.
then i used eventTrigger component to move on.

my main purpose here is to get the name of the button which the mouse is over.
eventsystem has a pointerEnter value on inspector. It changes when the mouse is on a diferent button.
In this context; i want to get the name of the button whenever i move the mouse over on a button.

Or a javascript sample for OnPointerEnter() function.

Sorry can’t serve with Javascript sample but shouldn’t be too hard to translate!
here is the c# version :

using UnityEngine;
using UnityEngine.EventSystems;


public class ButtonNameSample : MonoBehaviour, IPointerClickHandler {

    #region IPointerClickHandler implementation
    public void OnPointerClick (PointerEventData eventData)
    {
        Debug.Log (gameObject.name);
    }
    #endregion
   
}

you just need to implement the IPointerClickHandler (dont forget to use the using UnityEngine.EventSystems; ) and then you have to implement the public void OnPointerClick (PointerEventData eventData)
when you put the ButtonNameSample on the same GameObject where your buttos script ist then you get the button name with gameobject.name…this.name as you used it would just give you the name ob the script

1 Like

Thank you PBeast,

so far I translated the code in java as it is below.

#pragma strict

import UnityEngine.UI;
import UnityEngine.EventSystems;
// three buttons attached to canvas
var butto = new UnityEngine.UI.Image [3];

function Start ()
{
}

function Update ()
{
}   
class MouseOnButtons extends  MonoBehaviour implements IPointerEnterHandler
{
           
    function OnPointerEnter (eventData : PointerEventData)
        {
            Debug.Log ("It works");
            Debug.Log (gameObject.name);
        }
}

And I attached the script to the canvas where buttons are attached. But It doesnt print the message and the name of the buttons which the mouse is over.
Can you help me guys?

So what I see is that you might have this script attached to the canvas. You would need to attach a copy of this script to each button. Then the button can detect the mouse enter.

i just changed the class name as script filename. It worked.
but still Debug.Log (gameObject.name); returns on console as “Canvas”.

thanks Ramcat,
I might use so many buttons on the scene and want to do it in one script.

now if can get the name of the buttons instead of “canvas” i can manipulate the relevant button.

     function OnPointerEnter (eventData : PointerEventData)
    {
        // here if i can match the relevant names   
                //Debug.Log (gameObject.name);
           for (i=0, i<=buttonArraySize,i++)
               {
                  if (butt[i].name==ButtonName)
                    {
                       //Manipulate relavant button
                    }
                }

        }

gameObject.name is not working as i planned.
is there a way to get the name of the button which the mouse is over, while the script is attached to canvas?

Yes, the code in my first post expects the script to be attached to the canvas and gets all the buttons in gameObject (the canvas). You’ll have to convert it to UnityScript.

I think the best way to go is attaching the script to each buttons under canvas.
this works for my purpose. case is done. :slight_smile:

thank you ramcat,
I have a go like this:

#pragma strict

import UnityEngine.UI;
import UnityEngine.EventSystems;
var butto : UnityEngine.UI.Image;
function Start ()
{
   
    butto = GetComponent(UnityEngine.UI.Image);
}

function Update ()
{
}
class klik extends  MonoBehaviour implements IPointerEnterHandler

{
    function OnPointerEnter (eventData : PointerEventData)
    {
       
        Debug.Log(butto.name);
       
    }
}