I can’t seem to figure this out. I must be doing something wrong.
I’ve got several buttons on the screen. I only want button1 to do something, all the other buttons should return miss, but when I run this code I get miss returns even when clicking on button1.
if (Input.GetMouseButtonDown(0)) {
Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
bool didHit = Physics.Raycast(toMouse, out hitInfo);
if (didHit) {
if (hitInfo.collider.name == button1.ToString())
{
Debug.Log("button1");
}
else
{
Debug.Log("Miss");
}
} else {
Debug.Log("nothing");
}
}
Can you try using CompareTag instead of name, let us check if the problem is about button1.ToString() or not. Just create a new tag called “Tag1”, add your tag to your button ( assuming your button has a collider ), and do this :