All raycast and onmouse events not working with sprites or ui objects

I was trying to check if mouse is over an inventory item, but it did not working doing just doing a very basic

void OnMouseOver() {
        Debug.Log($"Mouse is over [{gameObject.name}]");
        //PanelController.hoverMouseInventory(bagItem.def.name, new Vector2(0, 0));
    }

So I decided to just do a broad raycast from mouse to check if anything is obstructing, and that finds absolutely nothing- collider or no collider, using this post as reference.

private void Update() {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue()), Vector2.zero, 0f);
        Debug.Log(hit.transform.name);
    }

(I ignored any checking, and just let it error out if nothing was ever hit. I just cared about whether anything was found or not) (I also tried changing Raycast distance to Mathf.Infinite, but that changed nothing)

I have multiple objects around my screen active with colliders, and not “is trigger” selected. Nothing is found, EXCEPT bullets from player or enemy. It makes no sense. Even if I make an object with everything nearly the same, the object does not get seen by raycast nor onmouse events. Bullet is an empty gameobject with a cube as a child with collider, while parent empty has physics and a script to make bullet move Test gameobject has everything including the layer the same, but on 1 object. Colliders, rigidbody, sprite with a visible layer (There is no background sprite/object).

You’re passing bad arguments to it such as direction zero and length zero. Whilst that’s appropriate for 3D physics, don’t use Raycast to check what a point overlaps in 2D. Use the dedicated OverlapPoint.

1 Like

I just tried using OverlapPoint (thanks for steering me closer to the right direction), but it had the exact same results as the prior method using Physics2D.Raycast, in that it for some reason would only find bullets. No other object in the game with 2D colliders and/or 2D rigidbodies (tried a mix of combinations just in case, including mixing up the layers).

    private void Update() {
        Collider2D hit = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue()));
        Debug.Log(hit.transform.name);
    }

The Image child of Item is just a generic Image, and the Quantity is just tmpro (I have tried with these 2 having both Is Trigger, enabled and disabled). I double checked the box collider 2D is covering the entire content that I am trying to hover over, but instead I get an error saying pretty much that nothing was found by the Physics.OverlapPoint found nothing (Except bullets- which I still cannot find what makes them special to be found which nothing else will be found).

[Update]
It dawned on me to try and use a layermask, but that did not work.

    private void Update() {
        Collider2D hit = Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Mouse.current.position.ReadValue()), LayerMask.GetMask("PopupUI"));
        Debug.Log(hit.transform.name);
    }

6335604--703383--ItemUIMakeup.PNG

I guess this fixes it for now for what interaction I was looking for. After some searching, I found that there is a bunch interfaces for IPoint, which gives me the mouse over/out interaction among many others (Albeit it, kind of annoying way)

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ItemUIController : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
    public TextMeshProUGUI quantity;
    public Image img;

    public Item bagItem;

    public void setQuantity(int newAmount) {
        quantity.text = newAmount.ToString();
    }

    public void OnPointerEnter(PointerEventData eventData) {
        PanelController.hoverMouseInventory(bagItem.def.name);
    }

    public void OnPointerExit(PointerEventData eventData) {
        PanelController.hoverMouseOutInventory();
    }

}

I have no idea why what you posted fixes your issue. Anyway, the main thing I saw that was wrong was the “ReadValue()” method call on a Vector3. Why is that added? Just pass in the Vector3. In the end, you must be passing in the wrong value to OverlapPoint if some other UI stuff fixed it.

It seems your issue then is related to input stuff and NOT 2D or 2D physics.

Because it turns out that my whole project was screwed up from the start. When you follow one bad tutorial, and you never learned the right way, you just set yourself up for failure. I had been setting up everything inside canvas- because what I had been taught in the past, was that is how all the 2D elements were rendered. My own fix, shouldn’t even be considered a real fix, and my problem should hopefully be solved when I start doing things the right way by not having every 2D element in canvas…