I have been trying to get a working isGrounded variable to get my character to stop from infinitely jumping can someone please show and/or explain what it is that I am doing wrong.

public class Player : MonoBehaviour
{

[SerializeField]
private float speed = 10.0f;
private float horizontalInput;
[SerializeField]
private float verticalInput;
[SerializeField]
private float jumpHeight = 5.0f;
private Rigidbody2D myrigidbody2D;
private bool isgrounded;
float Ypos;
float YposVerify;

// Use this for initialization
void Start()
{
    myrigidbody2D = GetComponent<Rigidbody2D>();
    isgrounded = false;
}

// Update is called once per frame
void Update()
{
    Movement();

    if (Input.GetKeyDown(KeyCode.Z) && isgrounded == true)
    {
        //transform.position += new Vector3(transform.position.x, transform.position.y + 20.0f,0);
        myrigidbody2D.velocity = new Vector2(0, speed * jumpHeight);

    }

}

private void Movement()
{
    transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
    horizontalInput = Input.GetAxis("Horizontal");

}

//make sure u replace "floor" with your gameobject name.on which player is standing
void OnCollisionEnter(Collision theCollision)
{
    if (theCollision.gameObject.name == "Ground")
    {
        isgrounded = true;
    }
}

//consider when character is jumping .. it will exit collision.
void OnCollisionExit(Collision theCollision)
{
    if (theCollision.gameObject.name == "Ground")
    {
        isgrounded = false;
    }
}

}

Another way to do it is with a raycast or spherecast straight down. It has the advantage of registering anything you might want to jump off of. Just have the cast reach a few centimeters past the bottom of your collider, and if it hits something, you’re grounded. If you want to retain the functionality of only registering the ground or certain objects, you can use layers.