Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question The local function 'OnCollisionEnter2D' is declared but never used

Discussion in 'Getting Started' started by unity_B334D70C57C393E20990, Aug 27, 2023.

  1. unity_B334D70C57C393E20990

    unity_B334D70C57C393E20990

    Joined:
    Aug 27, 2023
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.CompilerServices;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6.  
    7. public class CharacterCtrl : MonoBehaviour
    8. {
    9.     public int Speed;
    10.     public int JumpForce;
    11.     public GameObject Character;
    12.     public GameObject Coin;
    13.     public Rigidbody2D rb2D;
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.        
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
    24.         {
    25.             rb2D.velocity = Vector2.left * Speed;
    26.         }
    27.  
    28.         if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) | Input.GetKeyDown(KeyCode.RightArrow))
    29.         {
    30.             rb2D.velocity = Vector2.right * Speed;
    31.         }
    32.  
    33.         if (Input.GetKeyDown(KeyCode.Space))
    34.         {
    35.             rb2D.velocity = Vector2.up * JumpForce;
    36.         }
    37.  
    38.         void OnCollisionEnter2D(Collision collision)
    39.         {
    40.             Debug.Log("d")
    41.         }
    42.  
    43.     }
    44. }
    Here's my full code for context
     
  2. unity_B334D70C57C393E20990

    unity_B334D70C57C393E20990

    Joined:
    Aug 27, 2023
    Posts:
    5
    I tried putting a semicolon, didn't change a thing (cant believe i forgot)
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    488
    You have put the
    OnCollisionEnter2D
    method inside your
    Update
    method. You should move it out of there.
     
  4. unity_B334D70C57C393E20990

    unity_B334D70C57C393E20990

    Joined:
    Aug 27, 2023
    Posts:
    5
    Just tried that. Thanks! but now it says it has an incorrect signature???
    Oh, btw my editor version is 2022.3.8f1 if that helps
     
  5. unity_B334D70C57C393E20990

    unity_B334D70C57C393E20990

    Joined:
    Aug 27, 2023
    Posts:
    5
    Nvm got it! (google for da win)