RigidBody2D.Freezeposition help

Hello, I made a character movement script with RigidBody2D, and want to stop the object when it collides with another object. Currently, I can move the character left and right but when I run into the object, I get the message “enter” in the console but my position doesn’t freeze, even though in the RigidBody2D menu it shows that my X and Y positions are frozen. I can still move around.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rigidmove : MonoBehaviour
{
public bool Disheld = false;
public bool Aisheld = false;
public int distance = 5;
// Start is called before the first frame update
void Start()
{
Rigidbody2D rbdy = gameObject.GetComponent();
}

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

    Rigidbody2D rbdy = gameObject.GetComponent<Rigidbody2D>();
    if (Input.GetKey(KeyCode.D))
    {
        rbdy.transform.Translate(distance*Time.deltaTime, 0, 0);

    }

    if (Input.GetKey(KeyCode.A))
    {
        rbdy.transform.Translate(-distance * Time.deltaTime, 0, 0);

    }

    if (Input.GetKey(KeyCode.W))
    {
        rbdy.transform.Translate(0, distance * Time.deltaTime, 0);

    }

    if (Input.GetKey(KeyCode.S))
    {
        rbdy.transform.Translate(0, -distance * Time.deltaTime, 0);

    }

}

private void OnTriggerEnter2D(Collider2D collision)
{
    Rigidbody2D rbdy = gameObject.GetComponent<Rigidbody2D>();
    rbdy.constraints = RigidbodyConstraints2D.FreezePosition;
    print("enter");
}

private void OnTriggerExit2D(Collider2D collision)
{
    Rigidbody2D rbdy = gameObject.GetComponent<Rigidbody2D>();
    rbdy.constraints = RigidbodyConstraints2D.None;
}

}

You are using transform.translate - this is not a function of the Rigidbody2D. The Rigidbody.freezePosition will only freeze the Rigidbody. You have a few options. The one that will require the least tweaking is to instead create a bool called freeze. Instead of calling Rigbody2D.freezePosition, set freeze to true. In your Update() method, at the top, put:

if (freeze)
    return;

Also, you should put a variable of type Rigidbody2D at the top called rbdy and in a Start() method, set rbdy to GetComponent(). GetComponent<>() is something you should try to avoid calling as much as you can.

Thanks for the Replies guys! I will return with results once I try these options out.