OnMouseDown() won’t work with crosshair

Hello!
I’ve been making a puzzle game using unity and I followed this tutorial for picking up boxes:

Unfortunately, I still can’t pick up anything. I’ve put a crosshair in the middle of the screen to show where my cursor should be and any box with the code I should be able to pick up, but it won’t work. I’ve tried to change this to keyCode, but that picks up any box that can be picked up and places it in front of me, even if my cursor is not on it. I’ve tried many forum solutions and I can’t find anything. Here is my code: ```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUp : MonoBehaviour
{
public Transform theDest;

void OnMouseDown()
{

GetComponent().useGravity = false;
this.transform.position = theDest.position;
this.transform.parent = GameObject.Find(“Destination”).transform;
}

void OnMouseUp()
{
this.transform.parent = null;
GetComponent().useGravity = true;
}
}

make sure your object has rigidbody and a box collider, make sure that your script is on the boxes and use Debug.log

I have rigid body and colliders on all of my boxes that I want to be able to pick up but it still won’t work. I also put my script on all the boxes

Insert Debug.Log statements into the call OnMouseDown/Up callbacks. This will tell you whether they are firing or not.

If you have a UI element following your mouse, make sure that to untick Raycast Target.

Nothing comes up in the debug log when I click

Well there’s step one complete, you now know the callbacks aren’t getting called.

The next step is figuring out why it isn’t. I can think of two reasons off the top of my head:

  • You might be using the new input system, which requires a different method to detect mouse inputs on objects

  • The raycast from clicking is being blocked before it reaches the scene

My money is on the second reason. Is your crosshair part within a UI Canvas? You will need to make sure that none of these are blocking raycasts.

Yes I put my crosshair on a UI Canvas but even when I didn’t have my crosshair I still wouldn’t get result. Maybe I could change it so that when I press a key, anything within my crosshair will go to the destination, but with keyCode anything that I can pick up teleports in front of me and I don’t know how to do something like that.