GetComponent() Method Not Working Properly

I created a script that made a call to a Script component attached to a GameObject, from another GameObject, it was working perfectly fine, then I closed Unity and after opening it again it just showed a Null Reference Exception Error everytime the component was tried to be used.

public class EnemyDamage : MonoBehaviour {

    public float damage;
    public float damageRate;
    public float pushBackForce;

    private float nextDamage;
    private bool playerInRange = false;

    GameObject playerAyla;
    PlayerHealth playerAylaHealth;
    Animator enemyAnim;
    // Use this for initialization
    void Start () {
        nextDamage = Time.time;
        playerAyla = GameObject.FindGameObjectWithTag("Player");
        playerAyla.GetComponent<Animator>();
        playerAylaHealth = playerAyla.GetComponent<PlayerHealth>(); //Here I get the script Component attached to a different game Object
        enemyAnim = transform.root.GetComponent<Animator>();
    }
   
    // Update is called once per frame
    void Update () {
        if(playerInRange) Attack();
    }

    void OnTriggerEnter(Collider other){
        if(other.tag == "Player")
        {
            playerInRange = true;
        }
    }

    void OnTriggerExit(Collider other){
        if(other.tag == "Player")
        {
            playerInRange = false;
        }
    }

    void Attack(){
        if(nextDamage <= Time.time){
            if(enemyAnim != null) enemyAnim.SetTrigger("attack");
            playerAylaHealth.AddDamage(damage); // Here I try to call the AddDamage method I wrote in the Script Component
            nextDamage = Time.time + damageRate;
           

            PushBack(playerAyla.transform);
        }
    }
}

In the end I had to change the “GetComponent” method and use a FindObjectOfType, which I know it’s not the best option… Any thoughts on why is this happening?

Your code snippet doesn’t show exactly where you used FindObjectOfType() or what line the null reference exception was on.

If it was where you’re calling “playerAyla = GameObject.FindGameObjectWithTag(“Player”)” and then trying to get the Animator component then you probably didn’t have a valid reference to playerAyla, so your FindGameObjectWithTag is failing to find something tagged with “Player”.

If it was where you’re calling “playerAylaHealth = playerAyla.GetComponent()” and then you can’t call AddDamage later in the script, then that gameobject didn’t have a component of type “PlayerHealth” on it, so it probably wasn’t added to the playerAlya gameobject when you saved the scene. If “PlayerHealth” was on a child gameobject somewhere underneath playerAyla, you’ll need to find the component on the gameobject or it’s children using “GetComponentInChildren()”.

Alternatively, you could make those properties public and assign them using the inspector so the references are saved with the scene.

I used FindObjectOfType() where I have the commets placed, instead of:
“playerAylaHealth = playerAyla.GetComponent();”

I had to write:
“playerAylaHealth = FindObjectOfType();”

The thing is before It was working perfectly fine, I was able to use the component with the GetComponent() method, it stoped working after I restarted Unity, of course, If I’m not getting anything in line 18 of my snippet, I understand I would get a Null Reference when trying to get the “AddDamage()” method from the PlayerHealth script, but the problem was that I was actually getting the component until I restarted Unity and never got it to work again.

Double-check that your player gameobject also has the PlayerHealth Monobehaviour on it. If it does, then you might have another object in your scene tagged as ‘Player’ that is being returned in the FindGameObjectWithTag and not the one you’re expecting.