I am making a click and drag game and I got the click and drag down, but my problem is the objects goes through the walls and I don’t want that to happen.
As it has already been mentioned in a comment, you shouldn’t set the transform.position directly when expecting physical behaviour from this movement.
Once you solved this using proper methods like the rigidbody’s AddForce methods, there might occur other issues such as bouncing behaviour at walls. And when pulled with enough speed, the objects will glitch through as well. You might need to consider another or at least an additional detection system using raycasts for this purpose, because, as stated, forcing a physics-driven object with enough speed to collide permanently with other objects looks weird.
And that is, because the physics engine calculates the new force which should normally be applied to the object (usually another direction) whereas the player might still force the object into the other direction. It looks like a strange and weird bouncing effect then.
This can be really tricky if you work against the physics engine.
Rather than move the object as you drag it, perform a sweep test from the current object to the new destination position. You can then use this to see if the object would collide with anything on it’s way to the new destination and react accordingly.
It may be useful to display a ‘ghost’ of the object under the cursor and if a collision occurs stop the ghost at the position of the collision, if no collision then the ghost will be located at the mouse cursor.
When the mouse is released, you then remove the ghost object and change the position of the actual object you wanted to move to it’s new position.