hello,
as the title says i cannot get the name of the UI Element Clicked. it is an Image…
i want to check if this was the gameobject clicked, but this seems difficult to do for some reason.
i mean
EventSystem.current.IsPointerOverGameObject() can detect if the mouse is over the image, so how would i be able to discern it from the other UI Component… i want to Cheak see if name == name but i cannot get its name… it always return null Reference, i think becuase it is an Image gameObject.
The .name property in an Image will return the name of the GameObject the Image is on.
The .name property on the Sprite that is being displayed by that Image will return the name of the sprite.
Without seeing some code I’m not sure what else to say about it.
Here is my Update function
void Update ()
{
// Check if the left mouse button was clicked
if(Input.GetMouseButtonDown(0))
{
// Check if the mouse was clicked over a UI element
if(EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Clicked on the UI");
cueBallUITransform.anchorMin = new Vector2(0.5f, 0.5f);
cueBallUITransform.anchorMax = new Vector2(0.5f, 0.5f);
cueBallUITransform.anchoredPosition = Vector2.zero;
gameObject.transform.localScale = centerScale;
}
if(!EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Did Not Click on GUI");
cueBallUITransform.anchorMin = new Vector2(1, 1);
cueBallUITransform.anchorMax = new Vector2(1, 1);
cueBallUITransform.anchoredPosition = initialPosition;
gameObject.transform.localScale = initialScale;
}
}
}
but this code work on any UI element i press, with no discernment
i want to add something like Blah == gameObject.name;
so that it only works if the specific gameObject is clicked not on all UI elements…
Your approach is combining two disparate input sources into one.
You are getting raw mouse input from the Input class, and you are processing the current event going through the Event System.
While this might ultimately work, it may prove difficult to support in the future, such as if you cover this UI with another layer, you will still get input via the Input class, which knows nothing about the UI.
You may wish to centralize on using the EventSystem and Unity UI objects exclusively.
That said, there is a .currentSelectedGameObject() method on the current EventSystem, so if it is non-null you should be able to use that to get its name and use it in your current setup.
1 Like
i think im going to have to change image to button and use .currentSelected()…
thank you for the help 