I’ve created a script that changes the material color of an object when you click on it. But I need the script to be shared among multiple objects, but only change the color of the object I click on. Currently, it changes the color of all the objects the script is attached to. The objects will all be the same prefab, just spaced across the screen.
public void Start () {
rend = GetComponent<Renderer>();
rend.enabled = true;
}
// Update is called once per frame
void Update()
{
if ( Input.GetMouseButtonDown(0) )
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if ( Physics.Raycast(ray , out hit , 100) )
{
rend.material.color = Color.green;
}
}
}
Is creating one script and attaching it to a prefab the wrong approach, or am I missing something else?