How to select objects with raycasts?

Im trying to convert a Transform into a custom type:

public class SceneMgr : MonoBehaviour
{
    public Building selectedBuilding;

    void HandleTerrainClicks()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                Transform target = hit.transform;
                selectedBuilding = target(???);
                SelectedBuildingSettings();
            }

how do I do this?

if( Input.GetMouseButtonDown(0) )
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if( Physics.Raycast(ray,out RaycastHit hit,1000f) )
{
Building hitBuilding = hit.collider.GetComponentInParent();
if( hitBuilding!=null )
{
selectedBuilding = hitBuilding;
SelectedBuildingSettings();
}
}
}