Player Health

So I have created a player health script it works great with enemy’s that i hand put in the game if I collide with an enemy or if it shoots me I loose health but when I use my spawner to spawn random enemy’s and I collide with and enemy I am getting a NullReferanceException taking me to this.

    public GameObject explosion;
    public GameObject playerExplosion;
    public int scoreValue;
    public int number;
    public Transform Powerup_speedBouns;
    public Transform Powerup_shield;
    public int attackDamage = 10;
    public GameObject Player;
    public PlayerHealth playerHealth;


void Attack()
    {

       
        if (playerHealth.currentHealth > 0) ///Here///
        {
            
            playerHealth.TakeDamage(attackDamage);
        }
    }
}

I noticed that in my prefab for my enemy I am not allowed to my player to playerHealth in the inspector which is why I am getting the error, but I can when it is in the scene, what am I doing wrong?

Here is my full script if it helps any.

using UnityEngine;
using System.Collections;

public class EnemyAttack : MonoBehaviour
{
    public GameObject explosion;
    public GameObject playerExplosion;
    public int scoreValue;
    public int number;
    public Transform Powerup_speedBouns;
    public Transform Powerup_shield;
    public int attackDamage = 10;
    public GameObject Player;
    public PlayerHealth playerHealth;

    private GameController gameController;

    void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find 'GameController' script");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Boundary") || other.CompareTag("Enemy"))
        {
            return;
        }

        if (explosion != null)
        {
           
            Instantiate(explosion, transform.position, transform.rotation);
            number = Random.Range(1, 50);
            Debug.Log(number);
            if (number == 1)
                Instantiate(Powerup_speedBouns, transform.position, Quaternion.identity);
            number = Random.Range(1, 50);
            if (number == 1)
                Instantiate(Powerup_shield, transform.position, Quaternion.identity);
          
        }

        if (other.tag == "Player")
        {
            Attack();
        }
        if (playerHealth.currentHealth <= 0)
           
        {
            Instantiate(playerExplosion, transform.position, transform.rotation);
            gameController.GameOver();
            Destroy(gameObject);
        }

        gameController.AddScore(scoreValue);
        Destroy(gameObject);
      
 
    }

    void Attack()
    {

        // If the player has health to lose...
        if (playerHealth.currentHealth > 0)
        {
            // ... damage the player.
            playerHealth.TakeDamage(attackDamage);
        }
    }
}

2486330--171443--Capture.PNG

Sounds like the spawned enemy may not have all its properties filled out, like you said, the player or playerHealth and so on. To figure out which one it is, look at the line number on the error you’re getting.

Now in order to fix it, what you need to do is when you Instantiate (spawn) the new enemy, get a reference to its GameObject, use .GetComponent<>() to retrieve its attack script, and then set up the necessary public values before allowing it to proceed.

One handy way to accomplish all this is to have a reference to the player in your Spawner script, and then when the Spawner spawns an enemy, it has the required information to set up the new enemy properly.

1 Like

you’re mixing up your scripts and trying to do things in the wrong places. As a concept your scripts should handle an aspect or behaviour, and if they need to affect something else they should call functions on scripts which handle that other behaviour.

So the basic “health” “attack” concept should be:

Health script:
handles starting / current health values
handles what happens when health is 0
exposes a function to reduce health / increase health

Attack script:
handles damage values, strike rates etc.
handles hit checking > on hit tells the associated health script on the thing being hit to take damage via it’s exposed function

2 Likes

Thank you, I will see what I can do.