0
\$\begingroup\$

I have a scene with a bunch of 2D doors that have 2d Box Colliders (no trigger) on them and that live on a Canvas. I would like to be able to click on them with the mouse, but have had no luck.

Following this earlier topic: (2D) Detect mouse click on object with no script attached , I did the following:

    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
        if (hit)
        {
            Debug.Log(hit.collider.gameObject.name);
        }
    }

However, (hit) is always null. I have no RigidBody2D objects in my scene, because I only need mouse selection, not any interaction between objects.

In the inspector I the green box collider but clicking within it in the scene (not the inspector, obviously) runs the MouseButtonDown() code but still no Raycast hit.

What else could I be missing?

enter image description here

To reduce this to its simplest, you can do the following:

  • Create a new 2D scene with a canvas
  • Add two 100x100 Images to the Canvas
  • Add a Boxcolllider2D to each image
  • Now what script is required to detect mouse clicks within them?
\$\endgroup\$
  • \$\begingroup\$ Instead of using ray.direction, try using vector3.forward \$\endgroup\$ – Savlon Mar 2 '16 at 5:03
  • \$\begingroup\$ Maybe you GameObject is over the mouse position? Maybe the Z value of the mouse is lower than the raycast start point? Not sure how the mouse codes work in unity... :P \$\endgroup\$ – BiiX Mar 2 '16 at 11:57
  • \$\begingroup\$ Try to simulate same scene without using canvas components but actual sprites. \$\endgroup\$ – S. Tarık Çetin Jul 10 '16 at 12:49
1
\$\begingroup\$

You should create Buttons instead of Images if you are working in Canvas. There you can assign image and events.

\$\endgroup\$
1
\$\begingroup\$

I am not sure what is wrong with your script as it seems fine, are you sure you have setup your images properly? I have the following setup in my inspector with the following code and it works.

Image Inspector

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
        if (hit)
        {
            Debug.Log(hit.collider.gameObject.name);
        }
    }
}

Another solution is to put a component on each door and inherit from IPointerClick\Down\UpHandler

Like this:

using UnityEngine;
using UnityEngine.EventSystems;

public class DoorViewController : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("Clicked on: " + gameObject.name);
    }
}

For more advanced things you might want to look at:

IPointerInputModule - I would link but the rules of this site are very draconian.

\$\endgroup\$
0
\$\begingroup\$

If you are open to having a script on the game object using the built in method OnMouseDown() works great to detect when clicked or tapped.

void OnMouseDown()
{
    // do stuff
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.