I have two game objects in my scene and I assign this script to them both. When I click on any of them the code inside the If statement related to each one gets executed twice, if I disable one of them, it gets executed once as it should.
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.name == "Object A")
{
Debug.Log("Object A Clicked"); // log twice
}
if (hit.transform.name == "Object B")
{
Debug.Log("Object B Clicked"); // log twice
}
}
}
}
The question is, why does the code gets executed twice with for a single click?!