# Help with script Error "Object reference not set"

**URL:** https://discussions.unity.com/t/help-with-script-error-object-reference-not-set/740020
**Category:** Scripting
**Tags:** Scripting
**Created:** [April 18, 2019, 4:24am UTC](https://discussions.unity.com/t/help-with-script-error-object-reference-not-set/740020 "2019-04-18T04:24:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Cohav0310](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cohav0310](https://discussions.unity.com/u/Cohav0310)
#### Post date: [April 18, 2019, 4:24am UTC](https://discussions.unity.com/t/help-with-script-error-object-reference-not-set/740020/1 "2019-04-18T04:24:28Z")

</div>

I know this is a generic problem, but I can’t wrap my head around how to fix it.  
the problem is on line 36. “gameObject.GetComponent().health -= gunDamage;”

public class PlayerShoot : MonoBehaviour  
{  
public Camera fpsCam;  
public GameObject crosshair;  
public PlayerLook playerLook;

public EnemyBehavior enemyInfo;  
public float gunDamage;

// Start is called before the first frame update  
void Start()  
{

}

// Update is called once per frame  
void Update()  
{  
//RaycastHit hit;  
//Ray shootingRay = new Ray(fpsCam.transform.position, Vector3.forward);

Debug.DrawRay(transform.position, fpsCam.transform.forward);

if (Input.GetButtonDown(“Fire1”))  
{  
[//Debug.Log](https://Debug.Log)(“shoot”);  
RaycastHit hit;  
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit))  
{  
if (hit.collider.tag == “Enemy”)  
{  
gameObject.GetComponent().health -= gunDamage;  
}  
}

}

if (Input.GetButton(“Fire2”))  
{  
crosshair.SetActive(true);  
fpsCam.fieldOfView = 10;  
playerLook.mouseSensitivity = 5;  
}  
else if (Input.GetButtonUp(“Fire2”))  
{  
crosshair.SetActive(false);  
fpsCam.fieldOfView = 60;  
playerLook.mouseSensitivity = 160;  
}

}  
}

---

<div class="post-metadata">

### Author: ![csofranz](https://avatars.discourse-cdn.com/v4/letter/c/c89c15/32.png) [@csofranz](https://discussions.unity.com/u/csofranz)
#### Post date: [April 18, 2019, 6:02am UTC](https://discussions.unity.com/t/help-with-script-error-object-reference-not-set/740020/2 "2019-04-18T06:02:23Z")

</div>

Please use code tags. We can’t easily see which line is 36 otherwise.

But in cases like this, the issue can be easily narrowed down. You are stringing together multiple references, and most likely, theGetComponent() invocation returned null.

Try using more verbose code that is more likely to reveal errors in your code:

```csharp
EnemyBehaviour eb = this.gameObject.GetComponent<EnemyBehaviour>();
if (eb !=null){
  eb.health -= gunDamage;
}else{
   debug.Log("I have no enemy behaviour attched"):
}

```

… which leads to the second question: Why do you search the own game object (this.gameObject) for the enemyBehaviour script, and not the object that is attached to the collider? That is very likely the reason why the GetComponent() call returns Null and fails on the access to .health. You are accessing the wrong object.

I’m guessing here, but I’m quite convinced that you wanted to write

```csharp
hit.collider.gameObject.GetComponent<EnemyBehavior>().health -= gunDamage;

```

Using ‘this’ as on line 1 in the first example, even though not required, makes the code much, much more obvious, and reveals mistakes you’d overlook otherwise. This may sound silly, but if you get into the habit of always prepending a statement that starts with ‘gameObject’ or ‘transform’ with ‘this’, you’ll cut down your frustration-derived intake of alcohol and chocolate by a good 50% 🙂

---

<div class="post-metadata">

### Author: ![Cohav0310](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@Cohav0310](https://discussions.unity.com/u/Cohav0310)
#### Post date: [April 19, 2019, 3:31pm UTC](https://discussions.unity.com/t/help-with-script-error-object-reference-not-set/740020/3 "2019-04-19T15:31:03Z")

</div>

> [@csofranz](#):
>
> Please use code tags. We can’t easily see which line is 36 otherwise.
> 
> But in cases like this, the issue can be easily narrowed down. You are stringing together multiple references, and most likely, theGetComponent() invocation returned null.
> 
> Try using more verbose code that is more likely to reveal errors in your code:
> 
> ```csharp
> EnemyBehaviour eb = this.gameObject.GetComponent<EnemyBehaviour>();
> if (eb !=null){
> eb.health -= gunDamage;
> }else{
> debug.Log("I have no enemy behaviour attched"):
> }
> 
> ```
> 
> … which leads to the second question: Why do you search the own game object (this.gameObject) for the enemyBehaviour script, and not the object that is attached to the collider? That is very likely the reason why the GetComponent() call returns Null and fails on the access to .health. You are accessing the wrong object.
> 
> I’m guessing here, but I’m quite convinced that you wanted to write
> 
> ```csharp
> hit.collider.gameObject.GetComponent<EnemyBehavior>().health -= gunDamage;
> 
> ```
> 
> Using ‘this’ as on line 1 in the first example, even though not required, makes the code much, much more obvious, and reveals mistakes you’d overlook otherwise. This may sound silly, but if you get into the habit of always prepending a statement that starts with ‘gameObject’ or ‘transform’ with ‘this’, you’ll cut down your frustration-derived intake of alcohol and chocolate by a good 50% 🙂

Thank you so much. I am not that experienced with c#, so thanks for the tip about prepending statements.
