How do I write a script that increments a variable whenever I click an object?

I have a number of objects with colliders in my scene. They’re all using the same tag and I want to increment a variable whenever the player clicks one of these objects.

What is the way to go about this, any links or examples would be much appreciated!

I only know how to use

void OnMouseDown() so far, and only when the script is attached to the object being clicked

I want one script that will increment a variable whenever any of the objects are clicked.

void Update()
{
if (Input.GetMouseButtonDown(0)) // Get the left mouse button down
{
CheckRaycast();
}
}

        int incrementVariable = 0;
        void CheckRaycast()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, 1000f))
            {
                if (hit.transform != null)
                {
                    if (hit.transform.tag == "Tag")
                    { 
                       incrementVariable++;
                       // Do what you want here.
                    }
                }
            }
        }