Powerup Script to make player invincible?

Title says it all. I simply want to make my player invincible after getting a powerup for a short period of time, so he can’t die from touching the objects that kill him instantly. I tried to look this up on the answered questions that were asked a while back and I could not find someone who asked this question except one guy. It was in java script so it’s like a whole other language to me, also his script gives his player a health bar(which mine doesn’t have, I’m making an infinite runner). Anyways since this is in the answers section I don’t want to make a big discussion out of it, so let’s be direct please or I’ll get in trouble. Here’s my basic powerup script:

public class PowerupScript : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D other)
    {
       if (other.tag == "Player"){
         Destroy (gameObject);
       }
 
       {
 
      }
  }
}

This is basically a write-my-code question, which is impossible to answer without knowing how your health system works. So a pseudo-solution would be :

  • when the player hits the power-up, call a function on the player to set invincible
  • that function would set a boolean to true, and invoke a function to set that boolean back to false after a delay
  • in the health part, before deducting health there would be a check to see if the boolean is true; if not, then deduct health.

Power-up script

player.SetInvincible();

Player script

public float invincibleTime = 3.0f;
bool isInvincible = false;

public void SetInvincible()
{
	isInvincible = true;
	
	CancelInvoke( "SetDamageable" ); // in case the method has already been invoked
	Invoke( "SetDamageable", invincibleTime );
}

void SetDamageable()
{
	isInvincible = false;
}

// health part
if ( !isInvincible )
{
	health -= whatever;
}