So I am trying to make it so that when “Wheat” is clicked it will Debug.Log something (for now). The raycast doesn’t seem to be working, but the OnMouseDown is detecting it. Is there a way I can switch it to OnMouseDown, and if not, what is wrong with my Raycast code?
Any help would be appreciated, and thanks!
//code
private void Update() {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
Debug.Log("smt");
if (hitInfo.collider.gameObject.tag == "Wheat")
{
Debug.Log("tag");
}
}
}
}
OnMouseDown detects a click when over the game object with the script. If you click elsewhere, the wheat code is never called. You don’t need the Ray cast since you know you have clicked on wheat to get this far.
You can use this interface to make GameObjects clickable
The code looks fine to me. Just in case, have you checked that the object to click has the appropiate tag? Is the gameObject on a layer that’s not Ignore Raycast? Is your script attached to any gameobject?
If you’re using the New Input System, I believe you have to access Mouse.current.position.ReadValue() in order to get the right mouse position, but I’m not 100% sure.
I still don’t understand. Before, my OnMouseDown function was working, but now that’s not even working. Here is my OnMouseDown code
//code
void OnMouseDown()
{
playerItems.wheat += 1;
Debug.Log(“OnMouseDown”);
}
I have a boxcollider2d as well, and it’s not set to trigger.