How to change sphere color when clicked?

I am trying to change the color of a sphere when I click on it. I am using this code

void OnMouseDown ()
    {
    
		rigidbody.useGravity = true;
		renderer.material.color = Color.black;
		}
}

This is in a script that I have given to my sphere. I am thinking my problem may be the sphere not knowing it has been clicked or something?

You will want to add a SphereCollider component to your game object.

This should work - provided that the sphere has a collider, isn’t hidden by another collider and isn’t in the IgnoreRaycast layer. You should also make sure that the script is attached to the sphere (not to a child or parent), and that the shader doesn’t ignore the material.color parameter. There’s also an extra “}” in your code, but I suppose that it’s a typo in the question. NOTE: OnMouseDown isn’t emulated by touches - only a mouse or similar pointing device generates this event.