0

I'm working on a mobile game and I need to check if the user touches one of 2 game objects. My script looks like this:

using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine;

public class PlayerControl : MonoBehaviour, IPointerClickHandler {

    public void OnPointerClick(PointerEventData eventData) {

        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);

    }


}

The code I got from: this stackoverflow post, but it still doesn't work for me.

My 2 game objects have the script, a rigidbody 2d and a box collider 2d..

When I click on the gameobjects, it doesn't log to the console. And the event mask is correct.

Can someone help me?

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Please, don't ask this question again. You have done so 3 times already with all being marked as a duplicate and deleted by you. Leave a comment if the duplicate did not work for you. – Programmer Oct 03 '17 at 05:50
  • 1
    1.Just like mentioned in the duplicate, you are using 2D collider and must attach `Physics2DRaycaster` to the camera (Notice the 2D in it). 2.Your `PlayerControl` script must be attached to the GameObjects you want to detect clicks on. 3.You must have EventSystem in your scene. See duplicate for more info. – Programmer Oct 03 '17 at 05:57

2 Answers2

1

IPointerClickHandler is part of the EventSystem, so you'll need to have an EventSystem in your scene, as well as a Physics Raycaster attached to your camera to allow the EventsSystem interfaces to work with 3d objects.

-1

Try OnMouseDown() and add this script on the objects you want to click and the objects must have colliders. It should work fine. For more information read the documentation about OnMouseDown() on the link below: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

Nouman Waheed
  • 118
  • 1
  • 1
  • 6