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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How should I convert a 'method group' ground check into a boolean?

Discussion in '2D' started by Swimmtrunk, Jun 28, 2021.

  1. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    (Been waiting for awhile on moderation in Unity Answers, so I thought I'd try asking the question here as well) I've been working on a 2D ground check for a simple platformer game and I'm running into difficulties using the check itself, here's the code I have:

    Code (CSharp):
    1.      private bool jumpKeyWasPressed;
    2.      private Rigidbody2D rigidbodyComponent;
    3.      private BoxCollider2D groundColl;
    4.    
    5.      [SerializeField] private float jumpHeight = 2f;
    6.      [SerializeField] private LayerMask jumpableGround;
    7.      void Start()
    8.      {
    9.          rigidbodyComponent = GetComponent<Rigidbody2D>();
    10.          groundColl = GetComponent<BoxCollider2D>();
    11.      }
    12.      void Update()
    13.      {
    14.          if (Input.GetKeyDown(KeyCode.Space) && IsGrounded)
    15.          {
    16.              jumpKeyWasPressed = true;
    17.          }  
    18.      }
    19.      private void FixedUpdate()
    20.      {
    21.          if (jumpKeyWasPressed)
    22.          {
    23.              rigidbodyComponent.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    24.              jumpKeyWasPressed = false;
    25.          }
    26.      }
    27.      private bool IsGrounded()
    28.      {
    29.          return Physics2D.BoxCast(groundColl.bounds.center, groundColl.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    30.      }
    My error explains that "Operator '&&' cannot be applied to operands of type 'bool' and 'method group'" but I thought my IsGrounded function returns a bool. I'm still relatively new to coding and I'm unsure how exactly something like this can be converted correctly. Any help is appreciated!
     
  2. MichaelH55

    MichaelH55

    Joined:
    Jan 19, 2014
    Posts:
    19
    IsGrounded() is a Function so

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
     
  3. Swimmtrunk

    Swimmtrunk

    Joined:
    May 27, 2021
    Posts:
    22
    Glad this was really simple lol
    Thank you!