# GetComponent() -\> NullRefereceException ??

**URL:** https://discussions.unity.com/t/getcomponent-nullrefereceexception/422869
**Category:** Scripting
**Tags:** Scripting
**Created:** [September 12, 2010, 4:40pm UTC](https://discussions.unity.com/t/getcomponent-nullrefereceexception/422869 "2010-09-12T16:40:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![krearthur](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/krearthur/32/948220_2.png) [@krearthur](https://discussions.unity.com/u/krearthur)
#### Post date: [September 12, 2010, 4:40pm UTC](https://discussions.unity.com/t/getcomponent-nullrefereceexception/422869/1 "2010-09-12T16:40:37Z")

</div>

Hallo

My script(ReadShield.js) is in the screenshot below:

the highlighted line marks the critical location: **Sometimes** (not always) I get this error:  
**NullReferenceException**  
**UnityEngine.Component.GetComponent (System.String type)**  
**ReadShield.OnTriggerEnter (UnityEngine.Collider other) (at Assets\Scripts\ReadShield.js:11)**

It seems that unity does not always find the specified component or the “current” camera.

Please help me, I’m getting mad because of that.

![385319--13276--$unbenannt_146.png](https://europe1.discourse-cdn.com/unity/original/3X/f/9/f95ace1e65459582b434a43eae344ed88eed6d6c.png)

---

<div class="post-metadata">

### Author: ![Vicenti](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/vicenti/32/369474_2.png) [@Vicenti](https://discussions.unity.com/u/Vicenti)
#### Post date: [September 12, 2010, 6:57pm UTC](https://discussions.unity.com/t/getcomponent-nullrefereceexception/422869/2 "2010-09-12T18:57:29Z")

</div>

You may want Camera.main instead of Camera.current.

---

<div class="post-metadata">

### Author: ![krearthur](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/krearthur/32/948220_2.png) [@krearthur](https://discussions.unity.com/u/krearthur)
#### Post date: [September 12, 2010, 9:21pm UTC](https://discussions.unity.com/t/getcomponent-nullrefereceexception/422869/3 "2010-09-12T21:21:57Z")

</div>

Thank you, that helped me much! But what is the difference? Why does Camera.main work better than Camera.current?

---

<div class="post-metadata">

### Author: ![Dreamora](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/dreamora/32/91058_2.png) [@Dreamora](https://discussions.unity.com/u/Dreamora)
#### Post date: [September 12, 2010, 9:48pm UTC](https://discussions.unity.com/t/getcomponent-nullrefereceexception/422869/4 "2010-09-12T21:48:10Z")

</div>

camera.main = the camera with the MainCamera tag

camera.current = the camera that was rendering at that time (if you have 2 or are in the editor this makes a difference)
