How to access script from clicked gameobject

I’m making a turn based strategy game and I want to toggle a field in a script for a unit from another script that registers a mouseclick, but I don’t know how to check which unit was clicked. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clicked : MonoBehaviour
{

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit rayHit;
        

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit, Mathf.Infinity, unitLayer))
        {
            T34 selectedOne = rayHit.collider.GetComponent<T34>();
            selectedOne.selected = !selectedOne.selected;
        }
    }
}

}

As you can see “T34” is my placeholder currently, but it should be whichever unit was selected. It seems that T34 selectedOne = rayHit.collider.GetComponent<T34>(); is the bit that needs changing but otherwise I have no clue.
Hope my question makes sense.

The only thing I see ‘wrong’ here is the toggle itself. Note that you’re just setting it to false (assuming it’s a bool).
I’ll do:

RaycastHit rayHit;

if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), 
    out rayHit, Mathf.Infinity, unitLayer))
{
    T34 selectedOne = rayHit.collider.GetComponent<T34>();

    if(selectedOne != null)
        selectedOne.selected != selectedOne.selected;
}

but, why don’t you start by checking which object was clicked on whether it has or it has not the T34 component? like:

RaycastHit rayHit;

if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), 
    out rayHit, Mathf.Infinity, unitLayer))
{
    Debug.Log(rayHit.collider.gameObject.name);
}