Clickable object not properly working

Hello guys, so I have 3 clickable objects in my scene, they do the work that I want to be done but they are not properly working. What I mean but that, when I move my character and click it sometimes it does not work and you have to go further with the character and click it again. I also tried to make the sphere collider radius larger but it did not fix the problem. Here is my scirpt:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ButtonOnClickActive : MonoBehaviour {

	public GameObject canvas;

  private bool clicked;
 void OnMouseOver()
      if(Input.GetMouseButtonDown(0) && !clicked)
         clicked = true ;
          GetComponent<Renderer>().material.color =;

Any thoughts?

@D3Duck Yes it is the whole script. No other script attatched to the object and no the collider dont get deactivated. this is a print screen

link text

The clickable object is the one with the search icon…

Well I got a script that works. I explain what is going on through comments in the script. Hope it helps.

    [Header("Canvas related to clicked object")]
    public GameObject canvas;

    [Header("Needed to get the mouse position of the player")]
    public Camera cam;

    [Header("How far a player can hit an object from")]
    public float reachOfPlayer = Mathf.Infinity;

    private bool clicked = false;
    private Color originalColor;

    private void Update()
        //If left clicked
        if (Input.GetMouseButtonDown(0))
            //Perform our click function

    void ClickedOnObject()
        //Will hold the reference for the clicked object
        RaycastHit hit;

        //Find the position and direction from the mouse
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);

        //Using the direction and origin from the mouse(ray)
        //Output the hit object (out hit)
        //Max Distance the ray can go until it hits an object(reachOfPlayer)
        if (Physics.Raycast(ray, out hit, reachOfPlayer))
            //If the hit object is the parent of this script and it has not been clicked
            if (hit.transform.gameObject == gameObject && clicked == false)
                //Turn on our canvas

                //Make sure we set our object as clicked
                clicked = true;

                //Get our original object color
                originalColor = GetComponent<Renderer>().material.color;

                //Then change it to the selected color
                GetComponent<Renderer>().material.color =;

            //If the hit object is the parent of this script and it has been clicked
            else if (hit.transform.gameObject == gameObject && clicked)
                //Turn off our canvas

                //Make sure we set our object as not clicked
                clicked = false;

                //Then change our object's color back to its original color
                GetComponent<Renderer>().material.color = originalColor;