problem while removing rigidbody

there is an object with rigidbody by which I make the buttons turn using physics, at some points I delete the rigidbody and add again, this is very important and necessary!

here is the rotation script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class TestScript : MonoBehaviour
{
	public  Rigidbody det5;
	
	
	public void TaskOnClickDetail5()
	{
		

		det5.AddTorque(new Vector3(0, 100, 0), ForceMode.Acceleration);


	}
	public void TwoTaskOnClickDetail5()
	{
		
		det5.angularVelocity = Vector3.zero;



	}
	public void TaskOnClick1Detail5()
	{
		
	
		det5.AddTorque(new Vector3(0, -100, 0), ForceMode.Acceleration);

	}
	public void TwoTaskOnClick1Detail5()
	{

		det5.angularVelocity = Vector3.zero;

	}



}

script to remove rigidbody

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Swipe : MonoBehaviour
{

private GameObject det1;
private  ConfigurableJoint joint;
private GameObject rb;
 Rigidbody rb_det1;





void OnTriggerEnter(Collider enter)
{
	if (enter.tag == "terrain")
	{
		Debug.Log("Enter");
		
		                 
		det1 = GameObject.FindWithTag("detail");
		Destroy(det1.GetComponent<Rigidbody>());
		rb_det1 = det1.GetComponent<Rigidbody>();
		UnityEditorInternal.ComponentUtility.CopyComponent(rb_det1);

	}
}
void OnTriggerExit(Collider exit)
{
	
	if (exit.tag == "terrain")
	{
		Debug.Log("Exit");

		
		UnityEditorInternal.ComponentUtility.PasteComponentAsNew(det1);

		
				
	}
}

The problem is that I already can’t manage these objects after deleting since the rigidbody were deleted, how can I solve this problem so that whenever I delete the rigidbody it could be assigned to a new one, I will break through a lot, but it won’t work, help me very grateful

det1 = GameObject.FindWithTag(“detail”);
Destroy(det1.GetComponent());
rb_det1 = det1.GetComponent();
UnityEditorInternal.ComponentUtility.CopyComponent(rb_det1);

There are a few problems here, in this code snippet in OnTriggerEnter(), you’re destroying the component then trying to copy it.

Also, and more importantly, ComponentUtility.CopyComponent is built for use in the editor, it will likely not work at runtime.

I’m not really sure why you’re destroying the rigidbody component only to re-add, maybe instead you could try disabling it while you don’t want it active:

 void OnTriggerEnter(Collider enter)
 {
     if (enter.tag == "terrain")
     {
         rb_det1 = det1.GetComponent<Rigidbody>();
         rb_det1.detectCollisions = false;
     }
 }
 void OnTriggerExit(Collider exit)
 {
     
     if (exit.tag == "terrain")
     {
         Debug.Log("Exit");
         rb_det1.detectCollisions = true;
     }
 }

Hope this helps!