Toggling active between objects using a boolean

Hey guys,

I'm having a little difficulty with a unit selection system I'm working on. I have a script (see below) attached to two objects, and what I'd like is for the player to click on one, and it become 'activeUnit'. As a visual aid, I've made them change colours depending on if they are active or inactive.

My problem is this : I can select them fine, but for the life of me I can't devise a way to only select one at a time. When you click on object B, I need object A to be unselected, and visa versa.

If anybody could point me in the right direction I'd be very grateful.

private var activeUnit : boolean = false;

function OnMouseDown () {
    var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var select : RaycastHit;

    if (Physics.Raycast (ray, select, 100.0))
    {
        //print ("selected " +select.collider.gameObject.name);
        activeUnit = true;
    } else {activeUnit = false;}
}

function Update () {

    if (activeUnit==true) {
        renderer.material.color = Color.green;
    } else if (activeUnit==false){renderer.material.color = Color.red;}

}

First off, OnMouseDown already uses raycasting, so don't bother with that, unless you really need that distance of 100. I don't know what your script is called, or what else you have going on. I might do it this way, in JavaScript:

var otherUnit : Unit; 
@HideInInspector var _active : boolean = false;

function OnMouseDown () {
    _active = true;
    renderer.material.color = Color.green;
    otherUnit._active = false;
    otherUnit.renderer.material.color = Color.red;
}

However, I really don't feel that JS is cut off for this kind of stuff. If you use C#, there is an event system, which can improve the code in many ways. If you have interest in this, and don't understand it, feel free to ask any questions about it you want.

using UnityEngine;

public class Unit : MonoBehaviour {

// "new" allows you to override the deprecated Component.active,
// but is active even necessary?
new bool active;
new Renderer renderer;  

delegate void EventHandler ();
event EventHandler MouseDown;
static event EventHandler SwitchActive;

void Awake () {
    renderer = GetComponent<Renderer>();
    MouseDown += Activate;
}

void OnMouseDown () {if (MouseDown != null) MouseDown();}

void Activate () {
    if (SwitchActive != null) SwitchActive();
    active = true;
    renderer.material.color = Color.green;
    MouseDown -= Activate;
    SwitchActive += Deactivate;
}

void Deactivate () {
    active = false;
    renderer.material.color = Color.red;
    SwitchActive -= Deactivate;
    MouseDown += Activate;
}

}

var myCubes : GameObject[];

function Start()
{
for (var thisCube in myCubes)
{
thisCube.renderer.material.color = Color.red;
}
}

function Update () 
{
if (Input.GetButtonDown ("Fire1"))
{
 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var select : RaycastHit;

    if (Physics.Raycast (ray, select, 100.0))
    {
    for (var thisCube in myCubes)
    {
    if( select.collider.gameObject ==thisCube)
    {
    thisCube.renderer.material.color = Color.green;
    }
    else
    {
    thisCube.renderer.material.color = Color.red;
    }
}
}
}
}