Q: Add a script to a new game object during run time I am making a game in unity... my script cuts a mesh body into 2 other mesh bodies. How can I add a script to the new body during run time?
Each chunk needs to add a script of name XRGrabInteractable.
foreach(GameObject chunk in pieces){
                if(chunk.GetComponent<BoxCollider>()){//change to collider type of first object
                    Destroy(chunk.GetComponent<BoxCollider>());
                }
                //Add rigid body if not alread
                if(!chunk.GetComponent<Rigidbody>()){//Add Rigid body if not true
                    chunk.AddComponent<Rigidbody>();

                }

                //Add Mesh Colider(might interere with xr script that requires collider)
                if(!chunk.GetComponent<MeshCollider>()){//If no mesh colider mesh colider for new chunk
                    chunk.AddComponent<MeshCollider>();//Add Mesh colider if it doesn't exist
                    chunk.GetComponent<MeshCollider>().convex = true;//Change To Convex Mesh so that it doesn't fall through floor
                }
}


A: Use GameObject.AddComponent<ScriptName>():
foreach(GameObject chunk in pieces){
    chunk.AddComponent<XRGrabInteractable>();

    if(chunk.GetComponent<BoxCollider>()){//change to collider type of first object
        Destroy(chunk.GetComponent<BoxCollider>());
    }

    //Add rigid body if not alread
    if(!chunk.GetComponent<Rigidbody>()){//Add Rigid body if not true
        chunk.AddComponent<Rigidbody>();
    }

    //Add Mesh Colider(might interere with xr script that requires collider)
    if(!chunk.GetComponent<MeshCollider>()){//If no mesh colider mesh colider for new chunk
        chunk.AddComponent<MeshCollider>();//Add Mesh colider if it doesn't exist
        chunk.GetComponent<MeshCollider>().convex = true;//Change To Convex Mesh so that it doesn't fall through floor
    }
}


A: It turns out I just needed to use...
using UnityEngine.XR.Interaction.Toolkit;

