- Home /
Hard time getting mouse click to work
Using Unity 2017.1.
I just can't get it to work; even with the simplest scenes. Here is what I did:
Create a new 2D project.
Add a sprite to the project and drag it to Hierarchy to create a GameObject.
Add RigidBody2D component to this GameObject (not doing this step doesn't make a difference though).
Now I want it to do something on MouseDown. I tried the following two ways:
Add a script to this object. Add the following function to it:
void OnMouseDown() { }
Place a breakpoint and run the project. Click on the GameObject. No mouse down.
Add a new EventTrigger component. Add the following method to the script:
public void Strike(BaseEventData d) { }
Link this function to the EventTrigger component using Pointer Down event. Place a breakpoint on the function and run. Click on the GameObject. Still no mouse down.
Can anyone help me figure out what's wrong here?
Edit
Here is the complete script:
using UnityEngine;
using UnityEngine.EventSystems;
public class BoardController : MonoBehaviour
{
void OnMouseDown() //breakpoint on this line
{
}
public void Strike(BaseEventData d) //breakpoint on this line
{
}
}
Hi @Shujee can you post the entire script you're attaching to the sprite?
Im guessing your trying to get an event from the user pressing on the board?
I'd use Debug for this btw.
Answer by dakshesh1010 · Jul 27, 2017 at 06:46 AM
Do you have any type of collider on the object. OnMouseDown() doesn't work without a collider.
O$$anonymous$$G. That works. Is what you are saying true for EventTrigger too?
This presents another problem though. I'm now able to click on the Board, but the pieces now get off the board as soon as the game starts, because of the collider.
Answer by SteenPetersen · Jul 27, 2017 at 06:48 AM
I made an exact copy of the project and it works in mine if you add a collider, so I guess it must be an absent collider.