Issue with isGrounded bool method and groundCheck array

I’m trying to refine my code for a project for groundChecking and I’m having issues getting the groundCheck array to return true (I have to use a groundCheck array because my player is long and I can’t figure out how to make the groundCheck wider)

private bool isGrounded()
    {
        foreach(Transform groundCheck in groundChecks)
        {
            return Physics2D.OverlapCircle(groundCheck.position, groundCheckArea, groundLayer);
        }
    }

Error: ‘PlayerMovement.isGrounded()’: not all code paths return a value

It is as the error says, all paths your logic can take must return a value. If nothing is returned in the foreach loop (ergo, there are no members in it), then a value has to be returned afterwards.

The solution is just to return false otherwise:

private bool isGrounded()
{
	foreach(Transform groundCheck in groundChecks)
	{
		return Physics2D.OverlapCircle(groundCheck.position, groundCheckArea, groundLayer);
	}
	
	return false;
}

This is basic C# stuff so these things can be easily be found online.

Thank you very much! I’ll be sure to do more research about error messages if I run into one.

Ground checks should be done with sweep queries not overlap queries. You must always ensure that your player is DEFAULT_CONTACT_OFFSET away from any other geometry when moving it. For that reason, an overlap query should never work when performing a ground detection. Instead, cast your player’s collider downward. This will also solve the problem of your player being too “long”, as its complete shape will be used.

A sequence of overlap queries with collider stored in an array to perform a ground detection really isn’t a valid approach.

This works, made an array of raycast down

   bool IsGrounded()
   {
       for (int i = 0; i <= 2; i++)
       {
           if (Physics2D.Raycast(groundCheckPoints[i].position, Vector2.down, 0.2f, groundMask))
           {
               groundCheck[i] = true;
           }
       }
       if (groundCheck[0] || groundCheck[1] || groundCheck[2])
       {
           jumpDirection = Vector2.zero;
           return true;
       }

       else
       {
           return false;
       }
           
   }