I made an UI Image as button and I use a script for click:
void Start() {
}
void Update() {
}
void OnMouseDown() {
Application.Quit();
}
UI Image has the script.
I made an UI Image as button and I use a script for click:
void Start() {
}
void Update() {
}
void OnMouseDown() {
Application.Quit();
}
UI Image has the script.
For UGUI UI objects, instead of OnMouseDown, use OnPointerDown:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems; // Required when using Event data.
public class ExampleClass : MonoBehaviour, IPointerDownHandler // required interface when using the OnPointerDown method.
{
public void OnPointerDown (PointerEventData eventData)
{
Debug.Log (this.gameObject.name + " Was Clicked.");
}
}
Alternatively, this can also be handled in the Inspector by adding an Event Trigger component.
I was literally just experiencing this same problem. I found that if I put a Button component on it, that’ll fix it right up.
,you need a object whit a Event System component. Graphiic raycaster added to your canvas and raycast target enabled on your ui element you wanna the click