[SOLVED] Detecting Mouse Click On An Object in 2D Game

I have an object that is instantiated and spawns randomly and I want the user to be able to click or tap the object to destroy it. The problem is, I don’t know how to detect where a player is clicking.

I’ve tried to research this, but I can only come up with results for 3D, and attempting to modify the results using Physics2D.Raycast didn’t work, so I’m stuck.

Any help would be greatly appreciated!

– Chris

You can use the EventSystem and the IPointerClick interface.
Be sure to include a Physics 2d Raycaster on the camera, and that an EventSystem is in the scene.

3 Likes

I just don’t understand how any of that works. Like how would my code look and how would I add IPointerClick interface?

EDIT: Derp. Forgot to put the camera script onto the camera object. :sweat_smile: But I don’t know how to use the event systems in terms of code. Like, I just add it to the object and do what with the code exactly?

Well, make sure there is an Eventsystem in the hierarchy
Make sure the camera has a Physics 2D Raycaster component.

Then, in a script on your object:

using UnityEngine.EventSystems;

class TheClassName : Monobehaviour, IPointerClickHandler // I think that's right, just let auto complete help you

Okay, then you mouse-over the “IPointerClick” in the IDE and you should get some option to ‘implement the interface’ … so click yes to that.
Now you will have a method added, which may have some “throw not implemented()” something or other. delete the throw part and put your code there & test it. :slight_smile:

2 Likes

Thank you for the awesome answer! I implemented the interface and I am able to detect clicking on my moving 2d game object but what i cant get working is when you miss the game object i want to be able to detect if you didn’t click on the 2d game object also. So i want to click the game object to destroy it you get a point if you miss clicking it you lose a point.

QUOTE=“methos5k, post: 3118932, member: 901661”]Well, make sure there is an Eventsystem in the hierarchy
Make sure the camera has a Physics 2D Raycaster component.

Then, in a script on your object:

using UnityEngine.EventSystems;

class TheClassName : Monobehaviour, IPointerClickHandler // I think that's right, just let auto complete help you

Okay, then you mouse-over the “IPointerClick” in the IDE and you should get some option to ‘implement the interface’ … so click yes to that.
Now you will have a method added, which may have some “throw not implemented()” something or other. delete the throw part and put your code there & test it. :)[/QUOTE]

You can try adding this code in any script – maybe your scoring script.

void Update() {
   if(Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject()) {
       --score;
     }
 }

Might require a bit of work if there are UI objects that could be clicked while playing, too. :slight_smile: You’d have to make sure it wasn’t one of those. Depends what you need.

For future reference, you should post a new thread – even though it’s somewhat related. That way it makes more sense to the forum, searches, etc… and doesn’t confuse the OP.

1 Like

i will give this a shot thank you

You’re welcome (hopefully it works!) :slight_smile: heh.

using UnityEngine.EventSystems;

public class test : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("clicked");
    }
}
2 Likes

I know this is 6 years late, but when I put the IPointerClickHandler in my IDE it gives me an error instead of offering to implement the interface.

Please don’t necro post for your own typing mistakes. Fix them yourself using this technique:

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.