if a gameobject in clicked code example
Example 1: unity know when gameobject presed
void Update () {
if (Input.GetMouseButtonDown(0)) {
CastRay();
}
}
void CastRay() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
if (hit.collider !=null) {
Debug.Log (hit.collider.gameObject.name);
}
Example 2: unity if gameobject is clicked
void OnMouseDown()
{
Debug.Log("Mouse is down")
}
void OnMouseUp()
{
Debug.Log("Mouse is up")
}
void OnMouseClick()
{
Debug.Log("Mouse is clicked (down then up its 1 click)")
}