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 ?
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
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.