I have a code that works but the color changes no matter where I click on the screen. I would like to make it so that the color of the object only changes when I click on the object.
Here is the code
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (Physics.Raycast(ray, 100))
{
this.GetComponent<Renderer>().material.color = Color.green;
}
}
}
,I have a code that works but the object changes color no matter where on the screen I click. I would like to make it so that it would change color only when I click on the object
This is the code I have now
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (Physics.Raycast(ray, 100))
{
this.GetComponent<Renderer>().material.color = Color.green;
}
}
}