add component through script unity code example
Example: unity add component
//Use the AddComponent<T>() Method to add a component to your gameobject
GameObject gameObject;
gameObject.AddComponent<BoxCollider>(); //Component has to be an actual Unity component