C# Writing a script to highlight colliders when mouse passes over them

All in all, the script runs fine except I noticed that when I go over the object too quickly, it will still stay highlighted (by highlighted I mean the material.color changes to blue). Any ideas why this might be happening? Thanks in advance!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PanelHighlight : MonoBehaviour {

GameObject[] Panel;
GameObject HitObject;
Renderer HitRend;

Camera Cam;
RaycastHit Hit;

void Start () {

	GetComponent<Camera> ();
	Cam = Camera.main;

	Panel = GameObject.FindGameObjectsWithTag ("Panel");
	System.Array.Reverse (Panel);


void Update () {

	if (Physics.Raycast (Cam.transform.position, Cam.transform.forward, out Hit)) {
		HitObject = Hit.collider.gameObject;
		HitRend = HitObject.GetComponent<Renderer> ();
		HitRend.material.color = Color.blue;
		HitRend.material.color = Color.gray;


Since you change HitRend to get the new renderer here:
HitRend = HitObject.GetComponent ();
Probably when you move your mouse fast, in one frame you set an object to blue, and in the next the ray hits another object, thus never entering your else.

To solve this you probably need to store your last selected object in a different variable than the same you use to select.

Also, after you clear the selection, it is a good idea to free the variable too. This way your code won’t loop setting the same color over and over again.