Search Unity

onCollisionEnter2D doesn't work

Discussion in '2D' started by Mononiek, Apr 3, 2021.

  1. Mononiek

    Mononiek

    Joined:
    Apr 3, 2021
    Posts:
    1
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class restart : MonoBehaviour
    6. {
    7.     public GameObject gameOverText, RestartButton;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         gameOverText.SetActive(false);
    13.         RestartButton.SetActive(false);
    14.     }
    15.  
    16.     void onCollisionEnter2D(Collision2D col)
    17.     {
    18.         if (col.gameObject.tag.Equals("Enemy"))
    19.         {
    20.             Debug.Log("It does work!");
    21.             gameOverText.SetActive(true);
    22.             RestartButton.SetActive(true);
    23.             gameObject.SetActive(false);
    24.         }
    25.     }
    26. }
    27.  
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class testcollision : MonoBehaviour
    6. {
    7.     void onCollisionEnter2D(Collision2D coll)
    8.     {
    9.         Debug.Log("It works!");
    10.     }
    11. }
    I've probally been making a silly little mistake the whole time but I can't seem to find it.

    Someone please help
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    hms0589, pleasurehouse and Mononiek like this.
  3. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Visual Studio can help you with the C# naming rules.
    Microsoft Visual Studio Community is Free!!