How do I deselect an object when another is selected?

Okay so I have two cubes and I want it so when I click the first cube it is selected and when i click the second cube it deselects the first cube and selects the second cube. How would I go about doing this? Here is the first script I tried:

#pragma strict

var selected = false;
var clickCount = 0;
 
function Start () {
 
}
 
function OnMouseOver() {
    if(Input.GetMouseButtonDown(0)){
    clickCount = clickCount + 1;
    }
    if(Input.GetMouseButtonDown(0))
    {  
       if(clickCount%2==1)
       {
       selected = true;
       Debug.Log("Grid Peice Selected");
       }
    }
    if(Input.GetMouseButtonDown(0))
    {
       if(clickCount%2==0)
       {
              selected = false;
              Debug.Log("Grid Peice Unselected");
       }
    }
}

This is the second script I tried this time using raycast:

#pragma strict
 
var selected = false;
 
function OnMouseDown()
{
 
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 
    if(Input.GetMouseButtonDown(0))
    {
       if(Physics.Raycast(ray, 100))
       {
         Debug.Log("Selected");     
       } 
 
 
    }else{
    Debug.Log("Unselected");
    }
    }

First make a new tag “cube” and assign it to all the cubes .

Name them to Cube_1 , Cube_2 and so on…and attach this script to any object in your scene

#pragma strict

function Update()
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;

	if(Input.GetMouseButtonDown(0))
    {
    	if(Physics.Raycast(ray,hit,1000))
		{	
		    if (hit.transform.tag == "cube")
		    {
		    	hit.transform.tag="Untagged";
		    	hit.transform.renderer.material.color=Color.blue;
		    	var cubes: GameObject[];
		    	cubes = GameObject.FindGameObjectsWithTag ("cube");
		    	for (var cube : GameObject in cubes) 
				{
	   				cube.renderer.material.color=Color.white;
				}
				hit.transform.tag="cube";
		       	Debug.Log("Cube is "+hit.transform.name);
		       	
		       	if(hit.transform.name=="Cube_1"){
		       		//do something
		       		print("1");
		       	}else if(hit.transform.name=="Cube_2"){
		       		//do something
		       		print("2");
		       	}else if(hit.transform.name=="Cube_3"){
		       		//do something
		       	}else if(hit.transform.name=="Cube_4"){
		       		//do something
		       	}else if(hit.transform.name=="Cube_5"){
		       		//do something
		       	}//and so on...
		       	
		    }
		}
	}
}