Bullets doesnt do any damage

I have an melee enemy tagged as “enemy”.
enemy has two coliders
a capsule(physics) and a sphere(for attacking player in radius and istrigger) and a particlesystem,enemy has enemyhealth script attached.similarly bullet has capsule collider and set is trigger,ive attached enemyhealth script to bullet as enemy sphere and bullet capsule are triggers,(bullet doent have any particle system need fix for this too without raycast),bullets are destroyed on colliding with enemy but doent damage the enemy.

script used for enemy health
using UnityEngine;
using System.Collections;

public class enemyhealth : MonoBehaviour {
	public int health = 40;
	public int currenthealth;
	//public int score = 10;
	public AudioSource deathclip;
	ParticleSystem death;
	//CapsuleCollider Collider;
	bool dead;
	// Use this for initialization
	void Start () 
	{
		deathclip = GetComponent<AudioSource> (); 
		//Collider = GetComponent<CapsuleCollider> ();
		death = GetComponentInChildren <ParticleSystem> ();
		currenthealth = health;
		dead = false;

	}
 
	void Update()
	{
		
	}
	public void Takedamage(int amount )
	{
		if (dead)
			return;
		currenthealth -= amount;
		if
			(currenthealth <=0)
		{
			die ();
		}


	}
	
	// Up	date is called once per frame
	void die () 
	{  

		//GetComponent<NavMeshAgent> ().enabled = false;
		dead = true;
		Destroy(gameObject);
		//Collider.isTrigger = true;
		//deathclip.Play ();
		//death.Play ();
	}


}

script used for bullet

using UnityEngine;
using System.Collections;

public class mover : MonoBehaviour {
	public float speed;
	public int damage = 10;
	enemyhealth Enemyhealth;
	GameObject ENEMY;
	bool enemyinrange;

	// Use this for initialization
	void Awake () {
		
		GetComponent<Rigidbody>().velocity = transform.forward*speed;

		Enemyhealth = GetComponent<enemyhealth> ();
		 
		ENEMY = GameObject.FindGameObjectWithTag ("enemy");

	}
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject == ENEMY && Enemyhealth.currenthealth >=0)
		{
			enemyinrange=true;
		}
	
		//Destroy(gameObject);

	
	} 
	void OnTriggerExit(Collider other)
	{
		if (other.gameObject == ENEMY)
		{
			enemyinrange = false;
		}
	}
	void Damage()
	{
		Enemyhealth.Takedamage (damage);
	}

	// Update is called once per frame
	void Update () 
	{
		if (enemyinrange) 
		{
			Damage ();
		}
    }
}

Thanks

Disclaimer: I am something of a noob in general and VERY MUCH a noob with c# :slight_smile:
But here goes:
I am assuming that the enemyhealth is attached to the ENEMY game object, right?
In the bullet script, shouldn’t Lines 16 and 18 be switched around and modified so that the variable Enemyhealth refers to the “enemyhealth” script component of the “ENEMY” object?

ENEMY = GameObject.FindGameObjectWithTag ("enemy");
Enemyhealth = ENEMY.GetComponent<enemyhealth> ();     

I hope this goes in the right direction; please forgive me if I’m mistaken :slight_smile:

How many enemies are in your scene?
It looks the the “ENEMY” reference in your mover script uses FindGameObjectWithtag, then finds one such object in the scene, not all.

Also, when you use GetComponent in mover (which you said is the script on your bullets), you get the EnemyHealth on your bullet, not your enemy. So when enemyinrange = true, you call Damage which has a refecence to your bullet’s EnemyHealth making your bullet take the damage.

What you should do when the bullet hits an enemy is something like this.

void OnTriggerEnter(Collider other) {
   if(other.tag == "enemy") {
      if(other.gameObject.GetComponent<enemyhealth>()  != null) {
         other.gameObject.GetComponent<enemyhealth>().Takedamage(damageAmount);
      }
   }

}

Thanks again hyperi0n ,figured making the check while firing bullet was the problem.now i understand why gameobject == ENEMY gives null reference exception,checked foe tag “enemy” works fine now.

	void Awake () 
	    {
           GetComponent<Rigidbody>().velocity = transform.forward*speed;
		}
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "enemy")
		{

			GameObject enemyNew = other.gameObject;
			enemyhealth enemyHealthNew = enemyNew.GetComponent<enemyhealth> ();
			if (enemyHealthNew.current >= 0)
				enemyHealthNew.Takedamage (damage);
			}
		Destroy(gameObject);

	} 
}