Not identifying the collider?

Basically, in reference to my healthbar (which works perfectly fine), I have my enemy running up to me and attacking when (distance<1), however it’s not detecting any colliders at all, they all have character controller/rigidbody etc. It just doesn’t remove health upon impact. How do I do this?

This is my code.

@script ExecuteInEditMode()
var healthTexture : Texture2D;
var healthBorder : Texture2D;

enemy = GameObject.Find("Enemy");
player = GameObject.Find("Player");

var health : int = 100;
var adjust : int = 0;

function OnCollision(other:Collision)
{
	Debug.Log("Collided with " + other.gameObject.name);
    if (other.gameObject.tag == "Enemy")
    {
        other.gameObject.tag == "Player";
        	health -= 10;
    }
}

function OnGUI () {
	
	GUI.DrawTexture(Rect(0,Screen.height - 600,290,100), healthBorder);

	//Now on top we can put Health Bar texture
	adjust = health * 1.5;                       //adjusting texture size (width) /       health(100)
	GUI.BeginGroup(Rect(122,Screen.height - 565,adjust,15));
	GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
	GUI.EndGroup();

}

Look forward to your answers.

Likely the error is that you created a custom function OnCollision() instead of using the event OnCollisionEnter().

Also your if statement looks a bit strange to me. The other.gameObject.tag == “Player” line doesn’t do anything.

Is this script placed on your Player object? If so, then I’d expect to see something more like this:

function OnCollisionEnter (other : Collision) {
    if (other.gameObject.CompareTag ("Enemy")) {
        health -= 10;
    }
}

Collisions may be tricky: OnCollision events are sent to the scripts attached to both objects when the rigidbody hits the CharacterController, but not when the character hits the rigidbody. To detect the character hitting something, you must use OnControllerColliderHit in the character script (nothing is sent to the other object).

In order to apply damage to the other object when a character hits it, you must use something like this (character script):

function OnControllerColliderHit(hit: ControllerColliderHit){
  if (hit.gameObject.tag == "Enemy"){
    // call the function ApplyDamage in the enemy script:
    hit.gameObject.SendMessage("ApplyDamage",10);
  }
}

And you must add the ApplyDamage function to the enemy script:

function ApplyDamage(damage: float){
  health -= damage;
  if (health <= 0){
    // enemy died
  }
}

The same applies to the enemy-hitting-player case.