GetComponent returns null [SOLVED]

Hello everyone, I’m trying to make a reference of an object in another script, but throws me the Null Reference Exception problem. I have this code, the problem is in the line 26:

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

public class EnemyMovement : MonoBehaviour {
    public float speed = 10;
    private int i;
    private Vector3[] direction = new Vector3[4];
    PlayerHealth playerHealth;

    private void Awake()
    {
        i = (int)(Random.value * 4);
        direction[0] = Vector3.right;
        direction[1] = Vector3.left;
        direction[2] = Vector3.back;
        direction[3] = Vector3.forward;
        playerHealth = GetComponent<PlayerHealth>();
    }

    void Start () {
        InvokeRepeating("turn", 5f, 2f);
    }

    void FixedUpdate() {
        if(playerHealth.Attacking = false)
        {
            speed = 0;
        }
        transform.Translate(direction[i] * speed * Time.deltaTime, Space.World);
    }
    void turn()
    {
        i = (int)(Random.value * 4);
    }
}

I’m spawning enemies every 5 seconds, and the problem starts when I put some new enemy, aparrently the GetComponent method is always null.

Is there a PlayerHealth script component on the EnemyMovement prefab?

@Doug_B has the correct question, if the component is not on the same gameobject, the value is null. You have to target the correct gameobject and get the component off of it, but also note…

playerHealth.Attacking = false

Is not a comparison call. Either you need a == or you can do

if(!playerHealth.Attacking)
2 Likes

EnemyMovement is not a prefab.

PlayerHealth is another script, which have a public bool Attacking, and I want to access that variable, in EnemyMovement’s script

I did not understand at all, but I think that you have a point.
btw, I fix the == problem.

Indeed. I meant on the prefab that contains the EnemyMovement script.

The problem is you are not accessing that script on another object. You are requesting it on the same object. Hence my previous question.

Also, you need to pay attention to the point made by Brathnann above.

Try stepping your code through in the debugger. You will find it very useful to do so.

Also, have you had a chance to go through any of the large number of tutorials for Unity and C# that are available? It would serve you well I think. Just scroll to the top of the page and click on the “Learn” link. :slight_smile:

1 Like

Thank for your help, I fix the problem, I share you my solution, first of all

1.- I added a new Gameobject and an PlayerHealth var
2.- I found my “Player” with tag
3.- I assigned the PlayerHealth’s component to my var hello
4.- And finnally I could access to the var Attacking,

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

public class EnemyMovement : MonoBehaviour {
    public float speed = 10;
    private int i;
    private Vector3[] direction = new Vector3[4];
    GameObject player;
    PlayerHealth hello;

    private void Awake()
    {
        player = GameObject.FindWithTag("Player");
        i = (int)(Random.value * 4);
        direction[0] = Vector3.right;
        direction[1] = Vector3.left;
        direction[2] = Vector3.back;
        direction[3] = Vector3.forward;
        hello = player.GetComponent<PlayerHealth>();
    }

    void Start () {
        InvokeRepeating("turn", 5f, 2f);
    }

    void FixedUpdate() {
        transform.Translate(direction[i] * speed * Time.deltaTime, Space.World);
        if (hello.Attacking == true)
        {
            speed = 0;
        }
    }
    void turn()
    {
        i = (int)(Random.value * 4);
    }
}

Good to hear - glad you got it working. :slight_smile: