unity add component by script code example
Example 1: Add component object to gameobject unity
//to add a new ridgidbody
gameobject.AddComponent<Rigidbody>();
//to add a new meshCollider
gameobject.AddComponent<MeshCollider>();
//Original answer by MakerBenjammin6, cleanup by me.
Example 2: On add component unity
void Reset()
{
//Your code here
}