Reputation: 85
I am coding a clicker game and I do not want my program to calculate whenever I press the UI. I have tried using OnMouseDown, OnPointerDown and !EventSystem.current.IsPointerOverGameObject() but all of them ruin the clicker-feel because you can't spam click. Are there any shorter alternatives? Thank you in advance!
Edit: I've done all the things you've commented and I've found the problem. Whenever I click my mouse button, text appears with the number of coins I get per tap. Therefore, it counts as a UI element, making it not possible to spam-click. I have now turned off the "Raycast Target" for the text and now it works as it should. Thank you for your help!
Upvotes: 2
Views: 1076
Reputation: 10551
You can use Input class along with a check:
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");
}
}
}
Or you can use OnMouseDown on specific object where you want to detect the click but with a UI check: void OnMouseDown() {
// Check if the mouse was clicked over a UI element
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log("Clicked on the UI");
}
}
Upvotes: 1