Trying to create a temporary invincibility powerup

Hello!

I’m trying to create a power up that stops the player from taking damage for a limited amount of time. Think Mario Star, Halo Sheild, Whatever. Here’s what I think needs to happen:

First Set up a Invincibility Timer:

  var InvinceTimer : float =10;

Then in the update function count it down (Eventually it should be wrapped up in a function, but I’m just trying to see if this works)

InvinceTimer -= 1 * Time.deltaTime;

Last, do an if statement to check if the countdown is 0. If it is, apply damage on an enemy hit:

public function ApplyDamage(d:int)
{
if (InvinceTimer <= 0)
	
	{
		playerFear +=d;
		print("damaged by : "+d);
	}

}

So What currently happens: If the timer is set to 0 from the start, the player takes damage. If the timer is set to anything other than 0, the player doesn’t take damage. For some reason however, the countdown in update does not seem to be actually decremented based on time. What am I doing wrong? If someone has an example of what I’m trying to do, it’d be much appreciated!

I would aim for something like this.

         var invincetTime:int=10;   //how long invincibility would last in seconds
            var invinceExpire:float = 0   //this will be the actual that we check against to 
                                          // if we are still invincible or not
    
function makeInvincible()
{
        invinceExpire=Time.time+ invinceTime;
}
        
 function ApplyDamage(d:float)
{
        
                if( Time.time>invinceExpire)
        	    {
        	   	  playerFear +=d
        		  print ("damaged by: "+d+" points.");
        
        	    }
        	    else {print(transform.name+" is Invincible for "+(invinceExpire-time.time)+" seconds more.);
}