- Home /
Having trouble with a health script
I made 2 health scripts one for player and one for enemy the player script works just fine and the enemy script is registering the amount of damage taken but will not perform the death function.
here is the enemyhealth script
public int startinghealth = 100;
public int currenthealth;
Zombie_controller zomb;
bool isDead;
bool damaged;
void Awake () {
zomb = GetComponent<Zombie_controller> ();
currenthealth = startinghealth;
}
void Update () {
damaged = false;
}
public void IsDamaged (int amount)
{
damaged = true;
currenthealth -= amount;
if(currenthealth <=0 && !isDead)
{
Death();
}
}
void Death ()
{
isDead = true;
zomb.enabled = false;
}
}
AND HERE IS THE SCRIPT TO DEAL DAMAGE
public int Damage = 200;
float speed = 2000;
Rigidbody rb;
EnemyHealth enemyhealth;
GameObject zombie;
bool hit;
void Awake () {
zombie = GameObject.FindGameObjectWithTag ("Zombie");
rb = GetComponent<Rigidbody> ();
enemyhealth = zombie.GetComponent<EnemyHealth> ();
}
void Update () {
rb.AddForce (transform.forward * speed);
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag ("Zombie")) {
hit = true;
Damaged();
}
}
void Damaged ()
{
if(enemyhealth.currenthealth > 0)
{
enemyhealth.IsDamaged (Damage);
}
}
void OnTriggerExit (Collider other)
{
if (other.gameObject.CompareTag ("Zombie")) {
hit = false;
}
}
void OnCollisionEnter (Collision collision)
{
if(collision.gameObject)
{
Destroy (gameObject);
}
{
Debug.Log ("BulletDestroyed");
}
} }
Im trying to disable the navigation when the zombie is dead but it just keeps going and telling me there is a Nullreference in my enemyhelath script.
The null reference exception is the thing you need to deal with first. It may well be the whole problem.
The error message should be telling you what line the NRE is on, and that will tell you what variable(s) might be null. Add some protection there to catch and log the fact, so that then you'll know which thing is null.
Then you need to work out why it's null. It may be for example that there's no Zombie_controller
component being found at line 11 of the first bit of code. You could check that by adding a line 12 like this
if (zomb==null) Debug.LogError("No ZombieController");
Your answer
![](https://koobas.hobune.stream/wayback/20220612055051im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Space shooter tutorial: increase speed of asteroids over time 1 Answer
Need Help with changing skyboxes -1 Answers
SteamVR - Spawning Prefab on Controller,SteamVR - Spawning Objects on Controller 0 Answers
Wave Spawner not working |Does not detect if an object is destroyed 1 Answer
Multiple button name in a row? 1 Answer