Player Health

I am making a game where I have a player tagged “Player” and an enemy tagged “enemy”. I want to deduct health away from the player so that the player dies in 10 seconds. health will be deducted from the player when he collides with the enemy. I just need a script for the player as I all ready have a script to handle killing they enemy.

in Javascript;

var cTime : Int = 10f;

function OnTriggerEnter(me : Collider)
{
    if(me.gameObject.tag == "Player")
    {
        yield WaitForSeconds(cTime);
        Destroy(me.gameObject);
    }
}

you can also do it like this:

var health = 10;
 
function OnTriggerEnter( something : Collider )
{
   var timer=10;
 
   if(something.gameObject.CompareTag("Enemy"))
{
     for (var i= 0; i < timer; i++){
     health--;
     yield WaitForSeconds(1); 
	//this will deduct 1 health per second for 10 seconds
	}
}
}

place this on the player, check the “is trigger” option on the collider and add rigidbody to it.

here:

var health = 10;
var i; 
function OnTriggerEnter( something : Collider )
{
   var timer=10;
 
   if(something.gameObject.CompareTag("Enemy"))
	{
    	 for ( i= 0; i < timer; i++){
    	 health--;
    	 yield WaitForSeconds(1); 
	//this will deduct 1 health per second for 10 seconds
		}
	}
}
function OnTriggerExit( something : Collider )
{
	i = 10;
}

again. add this to the player and add rigidbody to both player and enemy.

please upvote so that we’ll be done here.

here in JS:

var PlayerHealth: float = 10.0;
var EnemyDamage: float = 1.0;//(damage per sec)You may change it to kill player quickly

function OnTriggerStay(enemy: Collider){
  if(enemy.CompareTag("Enemy"))
{
 PlayerHealth-=EnemyDamage*Time.deltaTime;
}
  if(PlayerHealth<0)
  {
  	Debug.Log("Player died.You lost!");
  	Destroy(gameObject);
  }
}

Remember to add Rigidbody to your character.