unity add component to game object via 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
}