How do I prevent objects from moving inside one another when carried

I have a simple script that uses Raycasting to pick up objects with a specific script, but the objects can still move through walls. I was just wondering how I might prevent this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickupObject : MonoBehaviour {
     GameObject mainCamera;
     bool carrying;
     GameObject carriedObject;
     public float distance;
     public float smooth;
     // Start is called before the first frame update
     void Start()
     {
         mainCamera = GameObject.FindWithTag("MainCamera");
        
        
     }
     // Update is called once per frame
     void Update()
     {
         if (carrying)
         {
             carry(carriedObject);
             checkDrop();
         }
         else
         {
             pickup();
         }
        
     }
     void carry(GameObject o)
     {
         o.GetComponent<Rigidbody>().isKinematic = true;
         o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
         carriedObject.gameObject.layer = 7;
     }
     void pickup()
     {
         if (Input.GetMouseButtonDown(0))
         {
             int x = Screen.width / 2;
             int y = Screen.height / 2;
             Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 5))
             {
                 Pickup p = hit.collider.GetComponent<Pickup>();
                 if (p != null)
                 {
                     carrying = true;
                     carriedObject = p.gameObject;
                 }
            
             }
         }
     }
    
     void checkDrop() {
         if (Input.GetMouseButtonUp(0)) {
             dropObject();
         }
         if (carriedObject == null)
         {
             Debug.Log("gone");
         }
 
        
     void dropObject() {
        
         carrying = false;
         carriedObject.gameObject.GetComponent<Rigidbody>().isKinematic = false;
         carriedObject = null;
     }
        
     }
 
}

Your object should have a collider that stops that from happening. Instead of setting it to kinematic, you could only make the collider smaller so it fits in the hands of the character without issues.
If that fails to work, a quick fix might be to simply extend your player collider forwards by a certain amount. It’s a quick and dirty fix, but might be enough in your case.

Thanks for the advice. Moving the collider forward didn’t seem to help, but how would I go about making the object collider smaller instead of kinematic?

Does your player have a non-kinematic rigidbody? If so you can attach the object to the player with a physics joint and together they will act like one big object.