Countdown in Seconds inside a 'while'

I have this piece of code and i need to put a countdown in SECONDS not by frame… l already try InvokeRepeting… but not work

// some code
// condition on while because curHeatLevel needs to decrease every time that Fire1 is not press
	
while (!Input.GetButton("Fire1") && curHeatLevel > 0 )
	{
		curHeatLevel--; // i need to decrease on every second here!
	}

float dt = 0.0f;
while (!Input.GetButton(“Fire1”) && curHeatLevel > 0 )
{
dt += Time.deltaTime;
if(dt > 1.0f)
{
curHeatLevel–;
dt -= 1.0f;
}
}

while (!Input.GetButton(“Fire1”) && curHeatLevel > 0 )
{
yield WaitForSeconds(1);
curHeatLevel–; // i need to decrease on every second here!
}

Easiest solution would be to make a update function that runs only once every second.

Have a look at [invoke repeating][1] function.

function Start()
{

     InvokeRepeating("ManageTurretHeat",1f,1f);
}

function ManageTurretHeat()
{
       if(!Input.GetButton("Fire1") && curHeatLevel > 0 )
       {
          curHeatLevel--;
       }
}

This method will work but might run unwanted so if there are 100 turrets, 100 times a second this function will be called regardless of if the turrent is hot or not. The performance loss wont be too much since the function hardly does anything other than a --. if you would like to handle it you can use Nested Invokes.

function Update()
{
    if(Input.GetButton("Fire1"))
    {
    // Do blah blah blah
       if(curHeatLevel == 0 && isInvoking("ManageTurretHeat") == false)
       {
           Invoke("ManageTurretHeat",1f);
       }
    }
}

function ManageTurretHeat()
{
       if(!Input.GetButton("Fire1") && curHeatLevel > 0 )
       {
          curHeatLevel--;
          Invoke("ManageTurretHeat",1f);
          
       }
      
}

But performance difference well you have test it. Im not sure which one runs fast.
[1]: Unity - Scripting API: MonoBehaviour.InvokeRepeating

i thing i got it… with this. anyway thanks guys.

#pragma strict

//var projetil : Rigidbody;
//var velocidade : int = 40;

var projectile : Rigidbody;
var velocidade : int = 20;
var taxaDeTiro : float = 0.07;
var tempoParaResfriar : float = 1;

private var nextFire : float = 0;
private var curHeatLevel = 0;



function Update () 
{

		
		if (Input.GetButton("Fire1") && Time.time > nextFire) 
		{
		
			nextFire = Time.time + taxaDeTiro;
			// Instantiate o projetil na mesma posicao e rotacao
			var clone : Rigidbody;
			clone = Instantiate(projectile, transform.position, transform.rotation);
			// adiciona uma velocidade no eixo Z... velocidade definida pela variavel 
			clone.velocity = transform.TransformDirection (Vector3.forward * velocidade);
			Destroy (clone.gameObject, 4);
			
			curHeatLevel++;
			print(curHeatLevel);
			
			if (curHeatLevel > 25 && curHeatLevel < 35)
			{
			taxaDeTiro = 0.3;
			print ("O Cannon está quente demais.");
			}
			else if (curHeatLevel >= 35)
			{
			taxaDeTiro = 5;
			curHeatLevel = 35;
			print("O canhão parou com " + curHeatLevel);
			}
			else
			{
			taxaDeTiro = 0.07;
			}	
		
		}

		

		if (!Input.GetButton("Fire1") && curHeatLevel > 0 )
		{
			InvokeRepeating ("Espera", tempoParaResfriar, 1);
		}

}


	function Espera ()
	{
		if (--curHeatLevel == 0);
		CancelInvoke("Espera");
		print("O meu Heat Level = " + curHeatLevel);
 	}