resetable Timer script - Just wondering if this would work or not.

Hi, I’ve been trying to make a script for a timer that, when the player isn’t near an enemy, would decrease and resets when the player gets close enough to an enemy. When the timer runs out it should damage the player. I found a script that seemed to be a good basis for it so I modified it a bit to fit my needs.

var totalTime : int = 100;
var timeLeft : float;

var timerText : GUIText;

function Start()
{
	timeLeft = totalTime;
}

function Update () 
{
	timeLeft -= Time.deltaTime;

	if ( timeLeft <= 0 ) 
	{
		//access script for damage here
		timeLeft = totalTime;
	}

	timerText.text = "Time till boredom penalty: " +timeLeft;
}  

function OnTriggerEnter(col : Collider)
{
	if(col.gameObject.parent.tag == "Enemy")
	{
		timeLeft = totalTime;
	}
}

I can’t get to my project right now so I can’t yet implement the damaging as I don’t know the script name etc. But besides that part, would this do what I want when put on a large sphere thats stuck on my player?

If you ran up next to an enemy and got into a fight, the timer would still go off. OnTriggerEnter only triggers when you first approach. To make it “remember” you were in range, could try:

int eCount=0; // # of enemies we are near
OnTriggerEnter() { if(blah, blah) eCount++; timeLeft=totalTime; }
OnTriggerLeave() { if(blah, blah) eCount--; }

Update() {
  // only countdown if no baddies near:
  if(eCount==0) {
    timeLeft-=Time.deltaTime;
    if(timeLeft<=0) ...
  }

A simpler idea would be to use OnTriggerStay to reset the timer. I assume that would run a little slower, but it’s only a 5-letter change from what you have now.