Function being called on multiple objects when just one is clicked

I have this code, calling a function when an object is clicked:

if (Input.GetMouseButtonDown(0))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("Hit " + hit.collider.name);
                this.clicked();
            }
        }

Function Clicked:

    void clicked()
    {
        Debug.Log("Clicked " + gameObject.name);
        if (controller.selectedColor == i)
        {
            timeTracker = Random.Range(3.0f, controller.timeMaximum + 1.0f);
            time = 0.0f;
            i = 0;

            activateRightColor();
            changed = false;
        } else
        {
            time -= 1.0f;
        }
    }

I have 3 objects that are clickable, and each one of them has 3 childs.
When I click on one object but the others have the same condition the function Clicked is called for all of them.

Why is it being called on all objects?

That’s because you are using the this keyword, which causes to call the clicked method of the class itself.

Instead, take the object returned by the raycast hit and call the function from there.

hit.collider.GetComponent<MyClass>().Clicked();

_
But, I encourage you to make the script containing the raycast detection a new one and attach it to other gameObject, like:
_

Script A, (in your three gameObjects): Contains the Cliked() method.

Script B, (in other new gameObject): the raycast detection logic.