2D When player gets hit make invulnerable for a few seconds

Hi I am using a CharacterController and would like to know what the best way to go about this is. Can I turn off or disable the collider on a CharacterController for a few seconds because that would more than likely solve this. Any help is appreciated and thank you in advance.

I am the poster of this question. I figured out a much better way for anyone reading this in the future. Still credit goes to ExplodingCookie for reintroducing me to yield.

var invincible : boolean = false;
var health     : int     = 5;

    function OnTriggerEnter (other : Collider)
    {
    	if (!invincible)
    	{
       	 if(other.gameObject.tag == "enemy")
        	{
        		hit = true; //used by me for other reasons, you can ignore
           	 	health -= 1; // subtract 1 form your total health
           	 	invincible = true; // makes this whole function unusable since invincible is no longer false
            	yield WaitForSeconds (3); 
            	invincible = false; // makes this whole function reusable since invincible is false again
        	}
    	}
    }

for any noobs wondering despite the function becoming invalid after invincible = true, the function will carry out all of its code even if it doesn’t currently meet the requirements.

I would suggest you add these new booleans at the top of your health script

var invincible = false;
var timeoutActive = false;

Then you go down to your OnTriggerEnter function and edit it as follows

function OnTriggerEnter (other : Collider)
    {
        if(other.gameObject.tag == "enemy")
        {
            hit = true;
            health -= 1;
            invincible = true;
        }
    }

Then add this into the update function

if(invincible) {
   if(timeoutActive) {
      BeginTimeout();
   }
}

Lastly, you create the BeginTimeout Function

function BeginTimeout () {
   timeoutActive = true;
   yield WaitForSeconds(3);
   invincible = false;
   timeoutActive = false;
}

You can replace the three with however long you wish your hit invincibility to last :wink: