Search Unity

Bug onCollisionEnter2D Not Working!! :(

Discussion in '2D' started by QueenMatilda, Jan 13, 2023.

  1. QueenMatilda

    QueenMatilda

    Joined:
    Jan 13, 2023
    Posts:
    3
    I try to use it to set a boolean to true so the player can enable jumping to avoid multi-jumps but the collision script doesn't seem to work. Been stuck on this for a while and can't seem to find an answer to it.

    Here's my code

    Code (CSharp):
    1. public class GroundCollisions : MonoBehaviour
    2. {
    3.    
    4.     public bool isGrounded;
    5.  
    6.     void FixedUpdate()
    7.     {
    8.         void onCollisionEnter2D(Collision col)
    9.         {
    10.  
    11.             if(col.gameObject.tag == "Player")
    12.             {
    13.                 UnityEngine.Debug.Log("Touched the ground");
    14.                 isGrounded = true;
    15.             }
    16.             else
    17.             {
    18.                 isGrounded = false;
    19.             }
    20.  
    21.         }
    22.     }
    23.  
    24. }
    ^code for ground

    Code (CSharp):
    1. if (Input.GetKeyDown("w")) //Makes the player jump with "W"
    2.         {
    3.             if (GetComponent<GroundCollisions>().isGrounded == true)
    4.             {
    5.                 UnityEngine.Debug.Log("Jump pressed");
    6.                 Lower_Chest.GetComponent<Rigidbody2D>().AddForce(new UnityEngine.Vector2(0, (JumpForce)) * Time.deltaTime, ForceMode2D.Impulse);
    7.             }
    8.         }
    ^code for the player (inside a FixedUpdate)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Check the capitalization on that method.

    The Visual Studios editor is supposed to catch stuff like that for you, it's called Intellisense.

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    Your OnCollisionEnter2D method is inside your FixedUpdate method. This makes it local to FixedUpdate only, so I doubt Unity will find it.
    Move it out of there.
     
    Kurt-Dekker and halley like this.
  4. QueenMatilda

    QueenMatilda

    Joined:
    Jan 13, 2023
    Posts:
    3
    Thank you very much it was the Capitalisation :D
     
    Kurt-Dekker likes this.