C# nullReferenceException help

I have been running into this issue for past few days where im supposed have enemy attack the player and player loses health via slider healthbar like in Surival shooter tutorial I went over the code but unsure whats wrong. the error shows up NullReferenceException: Object reference not set to an isntance of an object Attack.Awake()(at Assets/Attack.cs:19) i makred below with // where error is at

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>(); //error appears here
        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);
        }
    }
}

the code below this is the player health script incase that be need to assist

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;
    }
}

Do you have a gameobject named “Player” in your scene? Is it active? Is it being found? Chances are that is what is happening. You’re not finding your Player object. (note the caps also)

I’m not a big fan of GameObject.Find, but that is what your error is if it’s that line.

I dont really know how do it any other way thats only reason im doing it find game object, i looked my player is Cyclop_Player is has been Tag as Player layer Default so he should be active since i am able to control him and move him, or should i be using models name instead of the tag? I changed where Gameobject player to Player thinking might been the captalization but that seems not effected it neither.

If the gameobject is named Cyclop_Player, then GameObject.Find isn’t going to find it. It’s looking for Player.

If you want to find by Tag, you need to use FindWithTag

Just make sure it’s the only thing Tagged with Player.

To avoid using GameObject.Find, you can easily create references in the scene. I don’t know enough about your setup to tell you better ways to do it, but you can generally create references other ways. Otherwise, the above should fix your issue.

Null Ref exceptions are something you don’t need help debugging. The line the error points to has something in it that is null, thus making it impossible to reference. These are all basically solved in the exact same pattern.

Simply look at what is in the line and use Debug.Log() to find what is null, then work backward from there to figure out where/why it is null.

I have tried that I can’t figure out why its null I have all scripts attached to their characters and slider assigned to the player I still no clue where to keep looking.

cyclopsHealth = player.GetComponent<PlayerHealthBar>(); //error appears here
if (player == null) Debug.Log("Player null, so nothing will work.");
PlayerHealthBar phb = player.GetComponent<PlayerHealthBar>();
if (phb == null) Debug.Log("HealthBar null");

cyclopsHealth = phb;

if (cyclopsHealth == null) Debug.Log("cyclopsHealth was Null, because something prior to this was null");