NullReferenceException for taking damage

I must be having off day today but I am running my game, and i run into this issue where i get the error NullReferenceExpection: Object reference not set to an instance of an object PlayerHealthBar.TakeDamage (Int32 amount) (at Asset/PlayerHealthBar.cs:67) I have clue to why cause the amount but im looking at Survival shooter tutorial as base im not 100% sure whats going on to reason work fine seeing it then trying duplicate idea and not having it work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealthBar : MonoBehaviour {
    public int startingHealth = 100;
    public int currentHealth;
    public Slider healthBar;
    public AudioClip ScreamsShouts_Monster_Processed;
    Animator anim;
    AudioSource playerAudio;
    Cyclop_Player_Controller playerMovement;
    bool isDead;
    private void Awake()
    {
        anim = GetComponent<Animator>();
        playerAudio = GetComponent<AudioSource>();
        playerMovement = GetComponent<Cyclop_Player_Controller>();
        // playerAttack = GetComponentInChildren <PlayerAttacking> ();
        currentHealth = startingHealth;
    }
    public void TakeDamage (int amount)
    {
        currentHealth -= amount;
        healthBar.value = currentHealth;
        playerAudio.Play();
        if(currentHealth <= 0 && !isDead)
        {
            Die();
        }
    }
    void Die()
    {
        isDead = true;
        //playerAttacking.DisableEffects();
        anim.SetTrigger("Die");
        playerAudio.clip = ScreamsShouts_Monster_Processed;
        playerAudio.Play();
        playerMovement.enabled = false;
       // playerAttacking.enabled = false;
    }
}

i did try run a suggestion i read for attack where i change this line in code under void Awake
player = GameObject.FindGameObjectWithTag(“Player”);
i change upper part to
player = GameObject.Find (“Player”);
but i run into a sub error below:
NullReferancExecption: Object reference not set to an instance of an Object Attack.Awake() (at asset/attack.cs:19) be code line below so i revereted it back to orginal.the capsule collider for the enemy is turned on
cyclopsHealth = player.GetComponent();

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Attack : MonoBehaviour {
    public float timeBetweenAttacks = 0.5f;
    public int attackDamage = 10;

    Animator anim;
    GameObject player;
    PlayerHealthBar cyclopsHealth;
    enemyHealth enemyHealth;
    bool playerInRange;
    float timer;

    // Use this for initialization
    void Awake () {
        player = GameObject.Find("Player");
        cyclopsHealth = player.GetComponent<PlayerHealthBar>();
        enemyHealth = GetComponent<enemyHealth>();
        anim = GetComponent<Animator>();
    }
    // Update is called once per frame
    void OnTriggerEnter(Collider other)
    {
        if(other.gameObject == player)
        {
            playerInRange = true;
        }
    }
    private void OnTriggerExit(Collider other)
    {
    if(other.gameObject == player)
        {
            playerInRange = false;
        }
    }
    void Update () {
        timer += Time.deltaTime;
        if(timer >= timeBetweenAttacks && playerInRange && enemyHealth.currentHealth > 0)
        {
             Attacks ();
        }
        if(cyclopsHealth.currentHealth <= 0)
        {
            anim.SetTrigger("Death");
        }
    }
    void Attacks()
    {
        timer = 0f;
        if(cyclopsHealth.currentHealth > 0)
        {
            cyclopsHealth.TakeDamage(attackDamage);
        }
    }
}

When you have an error with a line number and you paste in the code , but the lines do not match up, it would be good to add a little comment in the provided code to say that is the line # in question :slight_smile:

Okay, so if you tag the player “Player” you can use FindGameObjectWithTag. Only mention that to clear that up.
Is the GameObject named “Player”?
Does it have a PlayerHealthBar script attached?

These few things have to be checked first, I think… :slight_smile:

sorry hadnt had chance reply over weekend, but yea they are all equiped to the characters and models they belong to.

Sry but can i also ask about this error that i had when i tried to equip a weapon by pressing V key ?

NullReferenceException: Object reference not set to an instance of an object
PlayerWeaponController.EquipWeapon (.Item itemToEquip) (at Assets/Scripts/PlayerWeaponController.cs:26)
InventoryController.Update () (at Assets/Scripts/InventoryController.cs:21)

but i cant seem to find out what i do wrong.
This is my PlayerWeaponController:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerWeaponController : MonoBehaviour {
    public GameObject playerHand;
    public GameObject EquippedWeapon { get; set; }

    IWeapon equippedWeapon;
    CharacterStats characterStats;

    void Start()
    {
        characterStats = GetComponent<CharacterStats>();
    }

    public void EquipWeapon(Item itemToEquip)
    {
        if (EquippedWeapon != null)
        {
            characterStats.RemoveStatBonus(EquippedWeapon.GetComponent<IWeapon>().Stats);
            Destroy(playerHand.transform.GetChild(0).gameObject);
        }
        EquippedWeapon = (GameObject)Instantiate(Resources.Load<GameObject>("Weapons/" + itemToEquip.ObjectSlug), playerHand.transform.position, playerHand.transform.rotation);
        equippedWeapon = EquippedWeapon.GetComponent<IWeapon>();
        equippedWeapon.Stats = itemToEquip.Stats;
        EquippedWeapon.transform.SetParent(playerHand.transform);
        characterStats.AddStatBonus(itemToEquip.Stats);
        Debug.Log(equippedWeapon.Stats[0].GetCalculatedStatValue());
    }

    public void PerformWeaponAttack()
    {
        equippedWeapon.PerformAttack();
    }
}

and this is my InventoryController:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryController : MonoBehaviour {
    public PlayerWeaponController playerWeaponController;
    public Item Sword;

    void Start()
    {
        playerWeaponController = GetComponent<PlayerWeaponController>();
        List<BaseStats> swordStats = new List<BaseStats>();
        swordStats.Add(new BaseStats(6, "Power", "Your Power Level."));
        Sword = new Item(swordStats, "Sword");
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.V))
        {
            playerWeaponController.EquipWeapon(Sword);
        }
    }

}

im stuck here since last week and i cant find out where i do wrong, so please hep !

In fairness, I think you should create a new thread and ask your question, to be fair to the OP and their question. :slight_smile:

Can you please comment/highlight the line numbers that are the problem, because your first code post doesn’t even have 67 lines.