Decreasing Player Health On Collision with Enemy

I’m wondering how I can get my script to work (In Java).

My HealthBar Script is as Follows (Using Unity 4.6 Canvas for the HealthBar):

var healthbar : GameObject;
var health : float;
var playerController : GameObject;

function Start () {

}

function Update () {
	playerController = GameObject.FindGameObjectWithTag("Player");
	healthbar.transform.localScale.x = health;
}

and my Zombie AI function to decrease the Player’s Health is:

function OnCollisionEnter(collision: Collision) {
 
	if (collision.gameObject.tag == "Player"){
		healthbar.health -= 0.1;		
	}
}

How would I go about on the Zombie’s collision with the Player decreasing the health for the Player?

Is the OnCollisionEnter on the zombie? Because it needs to be in the same script as the player’s in order to change the health variable. I recommend tagging your zombie as “Enemy”, and putting this on your player (not tested)

 var healthbar : GameObject;
 var health : float = 10;
 var playerController : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
     playerController = GameObject.FindGameObjectWithTag("Player");
     healthbar.transform.localScale.x = health;
 }

function OnCollisionEnter(collision: Collision) {
  
     if (collision.gameObject.tag == "Enemy"){
         healthbar.health -= 0.1;  
         Debug.Log(health);

  
     }
 }

Add a capsule collider to the Zombie and set it to “Is trigger” And instead of using:

function OnCollisionEnter(collision: Collision) {
  
     if (collision.gameObject.tag == "Player"){
         healthbar.health -= 0.1;        
     }
 }

use:

function OnTriggerEnter(collision: Collider) {
  
     if (collision.gameObject.tag == "Player"){
         healthbar.health -= 0.1f;        
     }
 }

Now should be good to go!