# GetComponent returns null \[SOLVED\]

**URL:** https://discussions.unity.com/t/getcomponent-returns-null-solved/710535
**Category:** Scripting
**Tags:** Scripting
**Created:** [August 5, 2018, 7:08pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535 "2018-08-05T19:08:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![reycaballo](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@reycaballo](https://discussions.unity.com/u/reycaballo)
#### Post date: [August 5, 2018, 7:08pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/1 "2018-08-05T19:08:00Z")

</div>

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:

```csharp
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.

---

<div class="post-metadata">

### Author: ![Doug\_B](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/doug_b/32/417686_2.png) [@Doug\_B](https://discussions.unity.com/u/Doug_B)
#### Post date: [August 5, 2018, 8:35pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/2 "2018-08-05T20:35:22Z")

</div>

> [@reycaballo](#):
>
> aparrently the GetComponent method is always null.

Is there a `PlayerHealth` script component on the `EnemyMovement` prefab?

---

<div class="post-metadata">

### Author: ![Brathnann](https://avatars.discourse-cdn.com/v4/letter/b/e5b9ba/32.png) [@Brathnann](https://discussions.unity.com/u/Brathnann)
#### Post date: [August 5, 2018, 9:11pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/3 "2018-08-05T21:11:53Z")

</div>

@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…

```csharp
playerHealth.Attacking = false

```

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

```csharp
if(!playerHealth.Attacking)

```

---

<div class="post-metadata">

### Author: ![reycaballo](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@reycaballo](https://discussions.unity.com/u/reycaballo)
#### Post date: [August 5, 2018, 9:12pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/4 "2018-08-05T21:12:50Z")

</div>

> [@Doug\_B](#):
>
> Is there a `PlayerHealth` script component on the `EnemyMovement` prefab?

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

---

<div class="post-metadata">

### Author: ![reycaballo](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@reycaballo](https://discussions.unity.com/u/reycaballo)
#### Post date: [August 5, 2018, 9:22pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/5 "2018-08-05T21:22:54Z")

</div>

> [@Brathnann](#):
>
> @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…
> 
> ```csharp
> playerHealth.Attacking = false
> 
> ```
> 
> Is not a comparison call. Either you need a == or you can do
> 
> ```csharp
> if(!playerHealth.Attacking)
> 
> ```

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

---

<div class="post-metadata">

### Author: ![Doug\_B](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/doug_b/32/417686_2.png) [@Doug\_B](https://discussions.unity.com/u/Doug_B)
#### Post date: [August 5, 2018, 9:23pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/6 "2018-08-05T21:23:32Z")

</div>

> [@reycaballo](#):
>
> EnemyMovement is not a prefab.

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

> [@reycaballo](#):
>
> PlayerHealth is another script, which have a public bool Attacking, and I want to access that variable, in EnemyMovement’s 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](https://forum.unity.com/members/brathnann.670510/) 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. 🙂

---

<div class="post-metadata">

### Author: ![reycaballo](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@reycaballo](https://discussions.unity.com/u/reycaballo)
#### Post date: [August 5, 2018, 11:21pm UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/7 "2018-08-05T23:21:19Z")

</div>

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,

```csharp
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);
    }
}

```

---

<div class="post-metadata">

### Author: ![Doug\_B](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/doug_b/32/417686_2.png) [@Doug\_B](https://discussions.unity.com/u/Doug_B)
#### Post date: [August 6, 2018, 6:34am UTC](https://discussions.unity.com/t/getcomponent-returns-null-solved/710535/8 "2018-08-06T06:34:45Z")

</div>

> [@reycaballo](#):
>
> Thank for your help, I fix the problem

Good to hear - glad you got it working. 🙂
