- Home /
Get OnMouseDown to work with a 2D Polygon Collider
I'm having a hell of time getting a 2D polygon collider to register a mouse click. I've attached images and code to show where I'm at. I cannot get the click to work. What am I doing wrong here? Need help!
using UnityEngine;
using System.Collections;
public class MouseClick : MonoBehaviour
{
void OnMouseDown()
{
Debug.Log ("Clicked the Collider!");
}
}
I've defined the collider:
I've setup the components for my image:
Answer by Glurth · Aug 11, 2015 at 02:13 PM
suggest you add the IPointerDownHandler interface to you MouseClick class: http://docs.unity3d.com/ScriptReference/EventSystems.IPointerDownHandler.html
You will also need to change the format of your handler function to match this interface.
public class MouseClick : MonoBehaviour, IPointerDownHandler
{
public void OnPointerDown(EventSystems.PointerEventData eventData);
{
Debug.Log ("pointer down while over the Collider!");
}
}
There are a whole bunch of these message handling Interface type classes: http://docs.unity3d.com/Manual/SupportedEvents.html
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Click On object that has a larger collider 1 Answer
Change controls from mouse to touch C# 1 Answer
How do i code a Mesh collider ? 1 Answer