My enemy is not taking damage...

I’ve tried to wrap my head around this for too long, so I’m just going to ask here :slight_smile:

I’ve made an Enemy Script that’s attached to the enemy in the scene: (The enemy script is called EnemyTest.cs and the enemy in the scene is called enemyPrefab.)

         using UnityEngine;
         using System.Collections;
          using UnityEngine.UI;


     public class EnemyTest : MonoBehaviour {

	private int health, maxHealth;
	public Image healthBar;

	void Start ()
	{
		healthBar = transform.FindChild ("EnemyCanvas").FindChild ("enemyHealthBG").GetComponent<Image> ();
		health = 100;
		maxHealth = 100;
	}

	void Update () {
	
	}

	public void Hit(int damage)
	{
		health -= damage;
		healthBar.fillAmount = (float)health / (float)maxHealth;
	}
     }

In my bullet prefab, I have this script attached. Bullet1.cs:

         using UnityEngine;
          using System.Collections;

       public class Bullet1 : MonoBehaviour {
	
	float speed;

	void Start () {
		Destroy (this.gameObject, 2);
		speed = 15;
		
	}


	void Update () {
		transform.position += transform.forward * Time.deltaTime * speed;
	}

	void OnCollisionEnter(Collision other)
	{
		if (other.gameObject.tag == "Enemy") 
		{
			other.gameObject.GetComponent<EnemyTest> ().Hit (10);
			Debug.Log ("Hit enemy");
			Destroy (this.gameObject);
		}
	}
        }

The bullet prefab does not have a tag, but the enemy is tagged as “Enemy”.

I have an enemy Canvas attached to the enemy too, with the health sliders and everything.
Why is this not working? I followed a tutorial on youtube (Unity 4.6 UI Tutorial - World Space Health Bars (+Projectiles) - YouTube)

Any help would be REALLY appreciated.
Cheers.

There are many factors that prevent "OnCollisionEnter " to be called… if the bullet is too fast, as @Benjames says, it will “jump” over the enemy collider. Check the layer collisison matrix on Edit > Project Settings > Physics. I don’t know how is your game mechanic but take a look on OverlapSphere for your bullet or Raycast to player gun…

//Try using OnTriggerEnter2D.Make sure the enemy box collider is triggered.

void OnTriggerEnter2D(collider2D other)

{

if (other.gameObject.tag == “Enemy”)

 {
     other.gameObject.GetComponent<EnemyTest> ().Hit (10);
     Debug.Log ("Hit enemy");
     Destroy (this.gameObject);
 }

}

}

Would it be better to have the OnCollisionEnter statement in the EnemyTest.cs? And tag the bullet as “Bullet1” or something?