how to add a component to a gameobject in unity script code example
Example 1: c# addcomponent
// Consider an existing GameObject, gameObj
Rigidbody addedComp = gameObj.AddComponent<Rigidbody>() as Rigidbody;
// You can also add custom components by class name
Example 2: On add component unity
void Reset()
{
//Your code here
}