onCollisionEnter2D doesn't work

This my first time making a game and I can’t get this to work.
I’ve spend at least 3 hours troubleshooting but I just don’t get it to work.

this is my code:

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

public class restart : MonoBehaviour
{
    public GameObject gameOverText, RestartButton;

    // Start is called before the first frame update
    void Start()
    {
        gameOverText.SetActive(false);
        RestartButton.SetActive(false);
    }

    void onCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag.Equals("Enemy"))
        {
            Debug.Log("It does work!");
            gameOverText.SetActive(true);
            RestartButton.SetActive(true);
            gameObject.SetActive(false);
        }
    }
}

I even tried to make a really simple script to see if that would work but it sadly doesn’t.
This is the code I used for it:

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

public class testcollision : MonoBehaviour
{
    void onCollisionEnter2D(Collision2D coll)
    {
        Debug.Log("It works!");
    }
}

I’ve probally been making a silly little mistake the whole time but I can’t seem to find it.

Someone please help

C# is case sensitive. It’s OnCollisionEnter2D not onCollisionEnter2D.

3 Likes

Visual Studio can help you with the C# naming rules.
Microsoft Visual Studio Community is Free!!