Jumping when on certain object

How do I make a jump system that only jumps when its touching an object with a specific tag.

Code:
if (Input.GetKeyDown(KeyCode.Space))
{
if (transform.position.y <= 4f)
{
Debug.Log(“Hit Ground”);
rb.GetComponent().AddForce(Vector3.up * JumpH);
}
}

The problem is that it can only jump when the objects y position is less than 4 which prevents you from jumping at a higher position. Im new to coding so I hope this is the right place to ask this. I dont know how to raycast either so that makes this harder.

You can use the istouching function of Collider2D to check if two colliders are touching. but I am guessing this is not what you are looking for.

So basically , you can use the OnCollisionEnter2D function to get the collider of the object the player is touching/entering into and then using the collider you get to find whether the gameobject if of the tag type you re looking for. But since you are using collisions instead of raycast you must also disallow player to jump again when he leaves the object using OnCollisionExit2D. Using the y axis will allow the player to jump more than once if he presses jump too quickly.

 void OnCollisionEnter2D(Collision2D collision)
    {
            if (collision.gameObject.tag == *Tag you are looking for *)
            {
    			playerAllowedToJump = true;
            }
    }

void OnCollisionExit2D(Collision2D other)
{
	if (collision.gameObject.tag == *Tag you are looking for *)
        {
			playerAllowedToJump = false;
	}
}