Only targeting when the mouse is over a GameObject

using UnityEngine;
using System.Collections;

public class Mouselock : MonoBehaviour {
    //The the selected gameobject
    public GameObject targeted;
    // how quickly you want to be able to look at the target.
    public float speed;
    // Use this for initialization
    void Start () {
        //Fills the array with GameObjects
        //uncomment this line if you do not need to check if new objects are being instantiated
        //targetable = GameObject.FindWithTag("Targetable");
    }
   
    // Update is called once per frame
    void Update () {
        //creates a ray from the mouse cursor to the camera
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        //detects to see if the ray hits an object with the tag Targetable and if the mouse is pressed down.
        Physics.Raycast(ray, out hit);
        if(hit.collider.tag == "Targetable" && Input.GetMouseButtonDown(0)){
        //selects the target
            targeted = hit.collider.gameObject;
        }
   
        // lock on target
        Quaternion targetRotation = Quaternion.LookRotation (targeted.transform.position - transform.position);   
        // Smoothly rotate towards the target point.
        transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, speed * Time.deltaTime);
    }
}

It looks at the object that I click on but it only looks at the object when the mouse is hovering over it.

Here is a solution

using UnityEngine;
using System.Collections;

public class Mouselock : MonoBehaviour
{
    //The the selected gameobject
    public GameObject targeted;
    // how quickly you want to be able to look at the target.
    public float speed;

    public bool canLookAtTarget;

    // Use this for initialization

    void Start()
    {
        canLookAtTarget = false;

        //Fills the array with GameObjects
        //uncomment this line if you do not need to check if new objects are being instantiated
        //targetable = GameObject.FindWithTag("Targetable");
    }

    // Update is called once per frame
    void Update()
    {
        //creates a ray from the mouse cursor to the camera
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        //detects to see if the ray hits an object with the tag Targetable and if the mouse is pressed down.
        bool HitedSomething = Physics.Raycast(ray, out hit);

        if (HitedSomething)
        {
            if (hit.collider.tag == "Targetable" && Input.GetMouseButtonDown(0))
            {
                //selects the target
                targeted = hit.collider.gameObject;
            }

            if(hit.collider.gameObject == targeted)
            {
                canLookAtTarget = true;
            }
            else
            {
                canLookAtTarget = false;
                targeted = null;
            }
        }

        if (targeted != null && canLookAtTarget)
        {
            // lock on target
            Quaternion targetRotation = Quaternion.LookRotation(targeted.transform.position - transform.position);
            // Smoothly rotate towards the target point.
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
        }
    }
}