Stopping a GUI score from continuously increasing

Hello,

I am still relatively new to Unity and I am having trouble with a couple of scripts.

At the moment, I have the following script attached to an object labeled “Mood” so that whenever the “Thought” object gets within a certain distance of the “Mood” object, the score increases:

var Mood : Transform;
var Thought : Transform;
var score : Score;

var distanceLimit : float = 5.0;

function Update() {
if (Vector3.Distance(Mood.position, Thought.position) < distanceLimit) {
score.AddToScore () ;
}
else{
}
}

I have this script linked to another script labeled Score, which looks like this:

var score = 0;
function AddToScore () {

++score;

guiText.text = "Score: " + score.ToString ();

}

Does anyone know if there is a way for me to have the GUI score stop increasing infinitely when the “Thought” object is no longer near the “Mood” object? My goal is to have the players score increase by +10 when a thought is near a mood, but your score will decrease by -10 when the thought is no longer near the mood.

Any help would be greatly appreciated! Thanks!

var Mood : Transform; 
var Thought : Transform; 
var score : Score; 
var scoreHasBeenAdded : boolean = false;

var distanceLimit : float = 5.0; 

function Update() { 
if (Vector3.Distance(Mood.position, Thought.position) < distanceLimit  !scoreHasBeenAdded) { 
score.AddToScore () ; 
scoreHasBeenAdded = true;
} 
}

I’ll try it out Starmanta, thanks!

Following on to StarManta’s code, you’ll probably want to reset the scoreHasBeenAdded when the Thought object moves away from the Mood object so that the next time the Thought and Mood objects approach the score is added again (unless you don’t want that behavior) :).

Jeff