# OnCollosionEnter not working

**URL:** https://discussions.unity.com/t/oncollosionenter-not-working/595433
**Category:** Scripting
**Tags:** Scripting
**Created:** [August 28, 2015, 11:39pm UTC](https://discussions.unity.com/t/oncollosionenter-not-working/595433 "2015-08-28T23:39:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![HarryIsBeast](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/harryisbeast/32/551137_2.png) [@HarryIsBeast](https://discussions.unity.com/u/HarryIsBeast)
#### Post date: [August 28, 2015, 11:39pm UTC](https://discussions.unity.com/t/oncollosionenter-not-working/595433/1 "2015-08-28T23:39:33Z")

</div>

So I recently started coding and using Unity so I’m not the best at it and I was stuck on this one. I have no idea why this will not work but I’m sure there is just something I didn’t notice. If anyone can tell me what I did wrong that would be great. If you want to know what I am trying to do, im trying to make a layer that if crashed into displays your health bar dropping, so the image fill amount goes down to 0.

```csharp
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Collision : MonoBehaviour {
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "AircraftJet")
        {
            Image image = GetComponent<Image>();
            image.fillAmount = .0f;
        }
    }
}

```

The console error is  
‘Script error: OnCollisionEnter  
This message parameter has to be of type: Collision  
The message will be ignored.’

---

<div class="post-metadata">

### Author: ![DenisLemos](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/denislemos/32/966855_2.png) [@DenisLemos](https://discussions.unity.com/u/DenisLemos)
#### Post date: [August 29, 2015, 12:03am UTC](https://discussions.unity.com/t/oncollosionenter-not-working/595433/2 "2015-08-29T00:03:32Z")

</div>

The class name should be the problem, “ **Collision** ” is already used by the unit. Rename the class and the script and test to see if this is the error.

---

<div class="post-metadata">

### Author: ![Suddoha](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/suddoha/32/110358_2.png) [@Suddoha](https://discussions.unity.com/u/Suddoha)
#### Post date: [August 29, 2015, 12:04am UTC](https://discussions.unity.com/t/oncollosionenter-not-working/595433/3 "2015-08-29T00:04:28Z")

</div>

> [@HarryIsBeast](#):
>
> So I recently started coding and using Unity so I’m not the best at it and I was stuck on this one. I have no idea why this will not work but I’m sure there is just something I didn’t notice. If anyone can tell me what I did wrong that would be great. If you want to know what I am trying to do, im trying to make a layer that if crashed into displays your health bar dropping, so the image fill amount goes down to 0.
> 
> ```csharp
> using UnityEngine;
> using UnityEngine.UI;
> using System.Collections;
> 
> public class Collision : MonoBehaviour {
> void OnCollisionEnter (Collision col)
> {
> if(col.gameObject.name == "AircraftJet")
> {
> Image image = GetComponent<Image>();
> image.fillAmount = .0f;
> }
> }
> }
> 
> ```
> 
> The console error is  
> ‘Script error: OnCollisionEnter  
> This message parameter has to be of type: Collision  
> The message will be ignored.’

Unity already provides a class called ‘Collision’. You could do some workarounds, creating your own namespace and using an alias name, but as you’re just about to write that class yourself, rename it.  
After that, the error shouldn’t appear anymore.

What actually happens in this situation: the parameter’s type is seen as your own Collision class, Unity ‘knows’ about the messages such as the OnCollisionXXXX ones and checks them, it expects to have Unity’s built-in type Collision (UnityEngine.Collision).

---

<div class="post-metadata">

### Author: ![HarryIsBeast](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/harryisbeast/32/551137_2.png) [@HarryIsBeast](https://discussions.unity.com/u/HarryIsBeast)
#### Post date: [August 29, 2015, 3:32am UTC](https://discussions.unity.com/t/oncollosionenter-not-working/595433/4 "2015-08-29T03:32:40Z")

</div>

@DenisLemos Do you know why this is not working?

```csharp
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class CollisionJawn : MonoBehaviour {
    void OnCollisionEnter (Collision col)
    {
        if(col.gameObject.name == "AircraftJet")
        {
            Image healthBar = GetComponent<Image>();
            healthBar.fillAmount = .0f;
        }
    }
}

```

This is my new error:  
NullReferenceException: Object reference not set to an instance of an object  
CollisionJawn.OnCollisionEnter (UnityEngine.Collision col) (at Assets/Scripts/CollisionJawn.cs:12)  
(And yes the file named Collision was renamed to CollisionJawn, not my idea)
