- Home /
Physics2D.OverlapPoint () return always null
Hi everyone, I'm trying to detect mouse click on 2D sprite on a 3D scene.
All my Sprite have a Box Collider 2D (well placed) and a script on it but hit is null all the time. I Also tried to put the Update() function on a script on GameEngine GameObject, but I got the same result.
void Update () {
if (Input.GetMouseButtonDown(0)) {
Vector2 mouse_position = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Collider2D hit = Physics2D.OverlapPoint (mouse_position);
if (hit) {
Debug.Log ("Hit" + hit.transform.name);
} else {
Debug.Log (hit);
}
}
}
void OnMouseDown() {
Debug.Log ("Hit " + this.name);
}
// I tried to update the sprite Z position, but didn't work neither
Answer by LoveZelda3 · Dec 08, 2016 at 06:46 AM
If anyone wonders why this function doesn't work with a EdgeCollider2D, as it didn't for me, Unity says on the website: https://docs.unity3d.com/ScriptReference/Collider2D.OverlapPoint.html that it doesn't support it: "This will always return false when used on an EdgeCollider2D.".
Now I found that I could use a PolygonCollider2D ins$$anonymous$$d, didn't know what they did before.
Answer by Spartiate0033 · Dec 07, 2016 at 11:24 PM
Why not make it a UI button? I notice the buildings are under the Canvas, a button would solve all of your issues and allow you to make easy animations/sounds when clicking on it. Really, REALLY easy to do.
The tutorial below is from 4.6, but it still largely functions the same.
https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button
Answer by Quasar47 · Oct 04, 2018 at 07:44 AM
I know this thread is a bit old, but for anyone who would come across the same issue, the error is that UIElements are incompatible with OnMouse() functions. Furthermore, BoxColliders will never work on UIElements because the canvas is not set in World space, as such, they are never considered as tangible objects, even with colliders. Instead, UIelements use an Interface called IPointerEnterHandler and IPointerClickHandler ; they are basically used like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class MouseOverUI : MonoBehaviour, IPointerEnterHandler, IPointerClickHandler, IPointerDownHandler {
public void OnPointerEnter(PointerEventData eventData)
{
//When the cursor enters a Ui's rect transform. Similar to OnMouseEnter().
}
public void OnPointerExit(PointerEventData eventData)
{
//When the cursor leaves a Ui's rect transform. Similar to OnMouseExit().
}
public void OnPointerClick(PointerEventData eventData)
{
//When the cursor clicks on a Ui's rect transform. Similar to OnMouseDown().
}
}
The same applies for OnMouseOver and OnMouseUp. Just add their respective interfaces after the keyword "MonoBehaviour" (look up for their documentations if you need them), then you can use their functions. You won't always need the eventData parameter, but always add it in the declaration of your function.
That's all, have a nice day ^^